简体   繁体   中英

How can we group by and sum based on headers? So Horizontal, not Vertical, Group By and Sum

I have this data frame.

                     2001Q1 2001Q2 2001Q3 2001Q4 2002Q1 2002Q2  ... 2011Q2  \
RCFD3531                  0      1      2      3      4      5  ...  14481   
RCFD3532                  0      0      0      0      0      0  ...      0   
RCFD3533                  0      0      0      0      0      0  ...      0   
RCFD3534                  0      0      0      0      0      0  ...      0   
RCFD3535                  0      0      0      0      0      0  ...      0   
                    ...    ...    ...    ...    ...    ...  ...    ...   
Unnamed: 115_level_0      0      0      0      0      0      0  ...      0   
Unnamed: 133_level_0      0      0      0      0      0      0  ...      0   
Unnamed: 139_level_0      0      0      0      0      0      0  ...      0   
Unnamed: 20_level_0       0      0      0      0      0      0  ...      0   
Unnamed: 87_level_0       0      0      0      0      0      0  ...      0   

                     2011Q3 2011Q4 2012Q1 2012Q2 2012Q3  
RCFD3531              14482  14483  14484  14485  14486  
RCFD3532                  0      0      0      0      0  
RCFD3533                  0      0      0      0      0  
RCFD3534                  0      0      0      0      0  
RCFD3535                  0      0      0      0      0  
                    ...    ...    ...    ...    ...  
Unnamed: 115_level_0      0      0      0      0      0  
Unnamed: 133_level_0      0      0      0      0      0  
Unnamed: 139_level_0      0      0      0      0      0  
Unnamed: 20_level_0       0      0      0      0      0  
Unnamed: 87_level_0       0      0      0      0      0  

[197 rows x 14487 columns]

Column names are:

2001Q1 
2001Q2 
2001Q3 
2001Q4 
2002Q1

I'm trying to group by these headers, and sum all values under these headers. I'm comfortable doing a group by and sum, vertically, but I have never done it horizontally before. I Googled this, and came up with the code below.

grouped_df = grouped_and_summed.groupby(grouped_and_summed.iloc[:0])
df_final = grouped_df.sum()
df_final = df_final.reset_index()

The data frame is named grouped_and_summed . It seems like this technique should work, but I'm getting this error:

ValueError: Grouper for '<class 'pandas.core.frame.DataFrame'>' not 1-dimensional

Of course there will be repeating columns. I'm trying to group by these repeating columns and do a sum of these repeating columns. I need to get the final result in ascending order as well? How can I do that?

df.stack().reset_index().groupby('level_1')[0].agg('sum')

Something like this.

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