熊猫分组根据条件

时间:2019-09-11 19:46:33

标签: python pandas group-by

我正在尝试在数据框中添加一列,以便为我提供客户帐户上的退货类型的计数。

这是数据框的外观:

CustomerID       Return$     Payment Method
000010           10          Credit Card
000010           15          Credit Card
000011           10          Check
000011           15          Credit Card
000011           10          Credit Card

这是预期的结果:

CustomerID     Return$   Payment Method   CC Return Count  Check Return Count
000010           10        Credit Card         2                  0
000010           15        Credit Card         2                  0
000011           10        Check               2                  1
000011           15        Credit Card         2                  1
000011           10        Credit Card         2                  1

这是我尝试过的代码,但它只为我提供了一个带有布尔值的列:

return_df['CC Boolean']= return_df.groupby(['CustomerID'])['Payment 
Method'].apply(lambda x: x=='Credit Card')

另一段代码给出了付款总数,与付款方式无关:

return_df['Counter']= return_df.groupby('Customer ID')['Payment Method'].transform('count')

2 个答案:

答案 0 :(得分:0)

method_dict = df.groupby('CustomerID')['Payment Method'].value_counts().unstack().fillna(0).to_dict()

df['CC Return Count'] = df['CustomerID'].map(method_dict['Credit Card'])
df['Check Return Count'] = df['CustomerID'].map(method_dict['Check'])

方法字典如下:

{'Check': {10: 0.0, 11: 1.0}, 'Credit Card': {10: 2.0, 11: 2.0}}

输出:

df

>>>


CustomerID  Return$ Payment Method  CC Return Count Check Return Count
0   10      10      Credit Card     2.0             0.0
1   10      15      Credit Card     2.0             0.0
2   11      10      Check           2.0             1.0
3   11      15      Credit Card     2.0             1.0
4   11      10      Credit Card     2.0             1.0

答案 1 :(得分:0)

您可以在groupby上使用CustomerID,然后为每个客户计算带有'Check''Credit Card'的行数。使用transform将保留原始结构:

df['check'] = df.groupby('CustomerID')['Payment Method'].transform(lambda x: sum(x == 'Check'))
df['credit'] = df.groupby('CustomerID')['Payment Method'].transform(lambda x: sum(x == 'Credit Card'))

输出:

   CustomerID  Return$ Payment Method  check  credit
0          10       10    Credit Card      0       2
1          10       15    Credit Card      0       2
2          11       10          Check      1       2
3          11       15    Credit Card      1       2
4          11       10    Credit Card      1       2