熊猫按条件分组

时间:2018-07-30 11:45:40

标签: python pandas group-by

这是我的数据-

FROM    TO  DIRECTION    AMOUNT  
B    A    IN    100
A    B    OUT    200
A    B    IN    300
B    A    OUT    40

作为输出,我想显示谁总共支付了谁的摘要-

FROM    TO    AMOUNT
A    B    300
B    A    340

请澄清一下,如果A-> B是第2行和第1行(IN是指从TOFROM的转移,OUT是指从{{1 }}到FROM

我在进行TO方式时遇到麻烦。我尝试过的-

.groupby()

但是当然这似乎并没有解决。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:3)

想法是根据条件的FROMTO的交换值:

mask = df['DIRECTION'] == 'IN'
df.loc[mask, ['TO', 'FROM']] = df.loc[mask, ['FROM', 'TO']].values
print (df)
  FROM TO DIRECTION  AMOUNT
0    A  B        IN     100
1    A  B       OUT     200
2    B  A        IN     300
3    B  A       OUT      40

然后聚合sum

df = df.groupby(['FROM', 'TO'], as_index=False)['AMOUNT'].sum()
print (df)
  FROM TO  AMOUNT
0    A  B     300
1    B  A     340

如果不想修改原始的DataFrame,则非常相似的解决方案:

mask = df['DIRECTION'] == 'IN'
df1 = df[['TO','FROM']].mask(mask, df[['FROM','TO']].values)
#output is same like above, only changed order of columns
print (df1)
  TO FROM
0  B    A
1  B    A
2  A    B
3  A    B

df2 = df['AMOUNT'].groupby([df1['FROM'], df1['TO']]).sum().reset_index()
print (df2)
  FROM TO  AMOUNT
0    A  B     300
1    B  A     340