复杂 Pandas Data Frame 的相同天数总和 [英] Sum of same days of complex Pandas Data Frame

查看:31
本文介绍了复杂 Pandas Data Frame 的相同天数总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该问题基于以下 SO:

解决方案

如果您的数据看起来像 上一个答案中的数据问题,错误是因为您有两列名为Day.由于它们似乎具有相同的数据,因此您可以删除最后一列,然后您的 groupby 将起作用:

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

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')

这篇关于复杂 Pandas Data Frame 的相同天数总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆