简体   繁体   中英

Sum of same days of complex Pandas Data Frame

The question has a base on the following SO:

Groupy brings only one key from Pandas dictionary

Dataframe looks like:

ALUP11  Return %    Day CESP6   Return %    Day TAEE11  Return %    Day
Data                                    
2020-08-13  23.81   0.548986    13.0    29.38   -2.747435   13.0    28.33   -0.770578   13.0
2020-09-01  23.68   1.067008    1.0     30.21   0.365449    1.0     28.55   1.205246    1.0
2020-08-31  23.43   -1.139241   31.0    30.10   -2.336145   31.0    28.21   -0.669014   31.0
2020-08-28  23.70   1.455479    28.0    30.82   1.615562    28.0    28.40   0.459851    28.0
2020-08-27  23.36   -0.680272   27.0    30.33   -1.717434   27.0    28.27   0.354988    27.0

After having the dataframe from dictionary, I need the sum of same days but

result = df.groupby('Day').agg({'Return %': ['sum']})
result

Get error:

ValueError: Grouper for 'Day' not 1-dimensional

For each symbol I would like to sum same days of month. In the example I have 3 symbols, so the result should be like:

If your data looks like the data in the answer to your previous question , the error is because you have two columns named Day . As they appear to have the same data you could drop the last column and then your groupby will work:

df = df.iloc[:, :-1].groupby('Day')

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