我在 pandas 数据框中有以下数据。某些日期值重复(2010-07-31、2010-10-31)。需要删除重复的日期并合并两行之间的值。
我已经使用了以下问题的解决方案。但使用下面的代码将日期列设置为索引。我不想将日期列设置为索引。 Need help in removing duplicate dates and merging values of two rows in Pandas Dataframe (python)
代码:
df = df.groupby(['Date']).sum()
数据框df
:
Index Date A B C D
1 2010-06-30 0:00:00 47.1 29.34 0.036 100.8
2 2010-07-31 0:00:00 47.1 29.34
3 2010-07-31 0:00:00 -4.644 100.2
4 2010-08-31 0:00:00 47.1 29.34 -1.481 100.4
5 2010-09-30 0:00:00 29.3 14.15 3.865 101.6
6 2010-10-31 0:00:00 29.3 14.15
7 2010-10-31 0:00:00 0.517 102.6
预期输出:
Index Date A B C D
1 2010-06-30 0:00:00 47.1 29.34 0.036 100.8
2 2010-07-31 0:00:00 47.1 29.34 -4.644 100.2
4 2010-08-31 0:00:00 47.1 29.34 -1.481 100.4
5 2010-09-30 0:00:00 29.3 14.15 3.865 101.6
6 2010-10-31 0:00:00 29.3 14.15 0.517 102.6
最佳答案
正如里斯提到的 -
df.groupby(['Date'], as_index=False).sum()
不会将日期列设置为索引
关于python - 删除 Pandas Dataframe 中的重复日期并合并值 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47063210/