[英]I have two columns in a pandas dataframe. One column has some same values (id) and corresponding values in another column has Number of tickets
id n_tickets
0 1586391 2
1 640 2
2 640 1
3 1181593 2
4 964842 1
5 780 1
6 780 1
I have the above data frame. 我有上面的数据框。 As you can see that in id column 640 and 780 have occurred two times. 如您所见,在id列640和780中已发生两次。 I want that just a single occurrence remains but there corresponding values in column n_tickets gets added up. 我希望只保留一次,但要在n_tickets列中添加相应的值。 My final dataframe should look like this: 我的最终数据框应如下所示:
id n_tickets
0 1586391 2
1 640 3
2 1181593 2
3 964842 1
4 780 2
I am using the code: df_tickets.groupby(['id','n_tickets']).sum()
but I am gettting the error: 我正在使用代码: df_tickets.groupby(['id','n_tickets']).sum()
但是我得到了以下错误:
Empty DataFrame
Columns: []
Index: []
when i use the below code: 当我使用以下代码时:
df_tickets.groupby('id',sort=False).sum().reset_index()
print(df_tickets.loc[df_tickets['id'] == 780])
I get this: 我得到这个:
id n_tickets
425166 780 1
985855 780 1
Instead I should get: 相反,我应该得到:
id n_tickets
425166 780 2
Try using: 尝试使用:
df_tickets = df_tickets.groupby(df_tickets['id']).sum()
This should work. 这应该工作。
You only need to group by 'id': 您只需要按“ id”分组:
df.groupby('id',sort=False).sum().reset_index()
Out[60]:
id n_tickets
0 1586391 2
1 640 3
2 1181593 2
3 964842 1
4 780 2
when i use the above code: 当我使用上面的代码时:
df_tickets.groupby('id',sort=False).sum().reset_index()
print(df_tickets.loc[df_tickets['id'] == 780])
I get this: 我得到这个:
id n_tickets
425166 780 1
985855 780 1
Instead I should get: 相反,我应该得到:
id n_tickets
425166 780 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.