![](/img/trans.png)
[英]Join two data frame with two columns values of a df with a single column values of another dataframe. based on some conditions?
[英]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
我有上面的数据框。 如您所见,在id列640和780中已发生两次。 我希望只保留一次,但要在n_tickets列中添加相应的值。 我的最终数据框应如下所示:
id n_tickets
0 1586391 2
1 640 3
2 1181593 2
3 964842 1
4 780 2
我正在使用代码: df_tickets.groupby(['id','n_tickets']).sum()
但是我得到了以下错误:
Empty DataFrame
Columns: []
Index: []
当我使用以下代码时:
df_tickets.groupby('id',sort=False).sum().reset_index()
print(df_tickets.loc[df_tickets['id'] == 780])
我得到这个:
id n_tickets
425166 780 1
985855 780 1
相反,我应该得到:
id n_tickets
425166 780 2
尝试使用:
df_tickets = df_tickets.groupby(df_tickets['id']).sum()
这应该工作。
您只需要按“ 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
当我使用上面的代码时:
df_tickets.groupby('id',sort=False).sum().reset_index()
print(df_tickets.loc[df_tickets['id'] == 780])
我得到这个:
id n_tickets
425166 780 1
985855 780 1
相反,我应该得到:
id n_tickets
425166 780 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.