Group duplicate columns and sum the corresponding column values using pandas [duplicate]

限于喜欢 提交于 2019-12-11 06:08:43

问题


I am preprocessing apache server log data. I have 3 columns ID, TIME, and BYTES. Example:

ID &nbsp &nbsp TIME &nbsp &nbsp BYTES

1 &nbsp &nbsp 13:00 &nbsp &nbsp 10

2 &nbsp &nbsp 13:02 &nbsp &nbsp 30

3 &nbsp &nbsp 13:03 &nbsp &nbsp 40

4 &nbsp &nbsp 13:02 &nbsp &nbsp 50

5 &nbsp &nbsp 13:03 &nbsp &nbsp 70

I want to achieve something like this:

ID &nbsp &nbsp TIME &nbsp &nbsp BYTES

1 &nbsp &nbsp 13:00 &nbsp &nbsp 10

2 &nbsp &nbsp 13:02 &nbsp &nbsp 80

3 &nbsp &nbsp 13:03 &nbsp &nbsp 110


回答1:


Let's try:

df['TIME'] = pd.to_datetime(df['TIME'])
ax = df.groupby('TIME')['BYTES'].sum().plot()
ax.set_xlim('13:00:00','13:03:00')

Output:



来源:https://stackoverflow.com/questions/48013007/group-duplicate-columns-and-sum-the-corresponding-column-values-using-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!