简体   繁体   中英

I want to group column names and add their values in a df

Here is the df that I am working with:

            2000-01    2000-02    2000-03   ...     2016-06  2016-07  2016-08
0            NaN        NaN        NaN   ...      590200   588000   586400
1       204400.0   207000.0   209800.0   ...      580600   583000   585100
2       136800.0   138300.0   140100.0   ...      209100   211000   213000
3        52700.0    53100.0    53200.0   ...      127400   128300   129100
4       111000.0   111700.0   112800.0   ...      192800   194500   195900
5       131700.0   132600.0   133500.0   ...      198200   199300   200600

I want to group each 3 months by quarter and add their values. So it should have such columns: 2000q1, 2000q2... and the values of 2000q1 should be the sum of 2000-01, 2000-02, 2000-03 values. etc...

Now I am doing this using for nest loops which is very inefficient and slow. Any idea how to make this much more efficient and shorter?

Setup

cols = pd.date_range('2000-01-31', '2001-08-31', freq='M').strftime('%Y-%m')
df = pd.DataFrame(1, index=range(3), columns=cols)

Solution

Convert with pd.to_datetime then with .to_period('Q') then groupby with axis=1

df.groupby(pd.to_datetime(df.columns).to_period('Q'), axis=1).sum()

   2000Q1  2000Q2  2000Q3  2000Q4  2001Q1  2001Q2  2001Q3
0       3       3       3       3       3       3       2
1       3       3       3       3       3       3       2
2       3       3       3       3       3       3       2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM