2021年1月16日星期六

Group By and Sum Horizontally

I am trying to do a Group By, based on quarter-end dates, and Sum all values below these quarter-end dates. I did some Googling before posting here. This is the line of code that I tried.

 grouped_and_summed = grouped_and_summed.groupby(axis=1, level=0).sum()  

The data frame is named grouped_and_summed and the axis, I think is 1, and the level, I think is 0. After I ran the code, I have the same grouped_and_summed.shape that I had before I ran the code. Here is a screen shot of my data, exported to a CSV file.

enter image description here

This: grouped_and_summed.head()

Gives me this:

           0       1       2       3       4       5      ...   14481   14482  \  index     2001Q1  2001Q2  2001Q3  2001Q4  2002Q1  2002Q2  ...  2011Q2  2011Q3     RCFD3531       0       1       2       3       4       5  ...   14481   14482     RCFD3532       0       0       0       0       0       0  ...       0       0     RCFD3533       0       0       0       0       0       0  ...       0       0     RCFD3534       0       0       0       0       0       0  ...       0       0                  14483   14484   14485   14486    index     2011Q4  2012Q1  2012Q2  2012Q3    RCFD3531   14483   14484   14485   14486    RCFD3532       0       0       0       0    RCFD3533       0       0       0       0    RCFD3534       0       0       0       0      [5 rows x 14487 columns]  

Somehow, I'd like to get rid of that very top row too. The one above the 'index'.

https://stackoverflow.com/questions/65756952/group-by-and-sum-horizontally January 17, 2021 at 11:07AM

没有评论:

发表评论