熊猫groupby并在不同类型之间使用数字

时间:2019-05-13 17:28:39

标签: python pandas dataframe

假设我有这样的df:

client   order_type    amount
John     Buy           100
John     Sell          100
Jeff     Buy           100
Jeff     Buy           100
Aaron    Buy           100
Aaron    Sell          100
Aaron    Buy           100

如果我这样做:

df.groupby(['client','order_type'])['amount'].sum()

我会得到类似的东西

John    Buy   100
        Sell  100
Jeff    Buy   100
        Sell  100
Aaron   Buy   200
        Sell  100

如何在新数据框中获取类似“买入-卖出”列的内容:

Name      NetBuy
John      0
Jeff      200
Aaron     100

2 个答案:

答案 0 :(得分:1)

只需将您的order_type映射到一个符号,有很多方法可以做到这一点,但我认为最容易阅读的是:

df['sign'] = [1 if x == 'Buy' else -1 for x in df.order_type]
df['amount_adj'] = df.sign*df.amount
df.groupby(['client'])['amount_adj'].sum()

输出:

client
Aaron    100
Jeff     200
John       0

使用单线和更快的np.where可获得相同的结果:

df = df.assign(amount=np.where(df.order_type.eq('Sell'), 
          df.amount*-1, df.amount)).groupby(['client'])['amount'].sum()

输出:

client
Aaron    100
Jeff     200
John       0

答案 1 :(得分:1)

首先将sell的值强制转换为负数,然后使用groupby.sum

df['amount'] = np.where(df['order_type'].eq('Sell'), -df['amount'], df['amount'])

df.groupby('client', as_index=False)['amount'].sum()

  client  amount
0  Aaron     100
1   Jeff     200
2   John       0