You can use groupby for columns:
df.groupby(np.arange(len(df.columns))
Or, they can be converted to date and time. You can use resample:
df.columns = pd.to_datetime(df.columns) df.resample('Q', axis=1).mean()
Here is a demo:
cols = pd.date_range('2000-01', '2000-06', freq='MS') cols = cols.strftime('%Y-%m') cols Out: array(['2000-01', '2000-02', '2000-03', '2000-04', '2000-05', '2000-06'], dtype='<U7') df = pd.DataFrame(np.random.randn(10, 6), columns=cols) df Out: 2000-01 2000-02 2000-03 2000-04 2000-05 2000-06 0 -1.263798 0.251526 0.851196 0.159452 1.412013 1.079086 1 -0.909071 0.685913 1.394790 -0.883605 0.034114 -1.073113 2 0.516109 0.452751 -0.397291 -0.050478 -0.364368 -0.002477 3 1.459609 -1.696641 0.457822 1.057702 -0.066313 -0.910785 4 -0.482623 1.388621 0.971078 -0.038535 0.033167 0.025781 5 -0.016654 1.404805 0.100335 -0.082941 -0.418608 0.588749 6 0.684735 -2.007105 0.552615 1.969356 -0.614634 0.021459 7 0.382475 0.965739 -1.826609 -0.086537 -0.073538 -0.534753 8 1.548773 -0.157250 0.494819 -1.631516 0.627794 -0.398741 9 0.199049 0.145919 0.711701 0.305382 -0.118315 -2.397075
The first alternative:
df.groupby(np.arange(len(df.columns))
Second alternative:
df.columns = pd.to_datetime(df.columns) df.resample('Q', axis=1).mean() Out: 2000-03-31 2000-06-30 0 -0.053692 0.883517 1 0.390544 -0.640868 2 0.190523 -0.139108 3 0.073597 0.026868 4 0.625692 0.006805 5 0.496162 0.029067 6 -0.256585 0.458727 7 -0.159465 -0.231609 8 0.628781 -0.467487 9 0.352223 -0.736669
You can assign this DataFrame:
res = df.resample('Q', axis=1).mean()
Change the column names as you like:
res = res.rename(columns=lambda col: '{}q{}'.format(col.year, col.quarter)) res Out: 2000q1 2000q2 0 -0.053692 0.883517 1 0.390544 -0.640868 2 0.190523 -0.139108 3 0.073597 0.026868 4 0.625692 0.006805 5 0.496162 0.029067 6 -0.256585 0.458727 7 -0.159465 -0.231609 8 0.628781 -0.467487 9 0.352223 -0.736669
And attach this to the current DataFrame:
pd.concat([df, res], axis=1)