简体   繁体   English

复杂 Pandas Data Frame 的相同天数总和

[英]Sum of same days of complex Pandas Data Frame

The question has a base on the following SO:该问题基于以下 SO:

Groupy brings only one key from Pandas dictionary Groupy 只带来了 Pandas 字典中的一个键

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:在这个例子中,我有 3 个符号,所以结果应该是这样的:

If your data looks like the data in the answer to your previous question , the error is because you have two columns named Day .如果您的数据与上一个问题的答案中的数据相似,则错误是因为您有两个名为Day列。 As they appear to have the same data you could drop the last column and then your groupby will work:由于它们似乎具有相同的数据,您可以删除最后一列,然后您的 groupby 将起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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