类似于下面
Buyer Seller Amount
John Mary 3
Mary John 2
David Bosco 2
我想将John和Mary的行合计为一个
预期出来
Trade1 Trade2 Amount
John Mary 5
David Bosco 2
我的数据框大约有6000行。谢谢您的帮助
答案 0 :(得分:1)
首先通过numpy.sort
对值进行排序,然后通过DataFrame.duplicated
创建布尔掩码,然后聚合sum
:
df[['Buyer','Seller']] = pd.DataFrame(np.sort(df[['Buyer','Seller']], axis=1))
df2 = df.groupby(['Buyer','Seller'], as_index=False)['Amount'].sum()
df2.columns = ['Trade1','Trade2','Amount']
print (df2)
Trade1 Trade2 Amount
0 Bosco David 2
1 John Mary 5
如果不想修改原始列,请使用语法糖-groupby
和Series
:
df1 = pd.DataFrame(np.sort(df[['Buyer','Seller']], axis=1))
df1.columns = ['Trade1','Trade2']
df2 = df['Amount'].groupby([df1['Trade1'],df1['Trade2']]).sum().reset_index()
print (df2)
Trade1 Trade2 Amount
0 Bosco David 2
1 John Mary 5