【问题标题】:Applying a function to each of the groups in groupby将函数应用于 groupby 中的每个组
【发布时间】:2023-01-10 15:02:18
【问题描述】:

我想对 .groupby 结果中的每个组应用一个函数。我原来的 df 看起来像这样:

Id  Cust_type   DPP
1   A           Detractor
2   B           Detractor
3   C           Detractor
4   D           Promoter
5   A           Promoter
6   B           Passive
7   B           Detractor
8   C           Detractor
9   D           Detractor
10  D           Promoter
11  A           Promoter
12  A           Passive

我想计算每个 Cust_types 的分数。完整 df 的分数是这样计算的:

((len(df[df['DPP']=='Promoters'])-len(df[df['DPP']=='Detractors']))/(len(df)))*100

所以我试图定义一个函数,然后将其应用于每个组,但下面的方法不起作用,因为我真的不知道该怎么做。

def score(x):
    return ((len(x[x['DPP']=='Promoters'])-len(x[x['DPP']=='Detractors']))/(len(x)))*100

df.groupby('Cust_type').apply(score, x))

任何帮助表示赞赏。

【问题讨论】:

  • 你能给我们你的预期输出吗?

标签: python pandas


【解决方案1】:

您的代码中有很多语法错误。

这是一个稍微简化的版本:

def score(x):
    return (x['DPP'].eq('Promoter').sum()-x['DPP'].eq('Detractor').sum())/len(x)*100

df.groupby('Cust_type').apply(score)

或者,当您只使用一个列时:

def score(x):
    return (x.eq('Promoter').sum()-x.eq('Detractor').sum())/len(x)*100

df.groupby('Cust_type')['DPP'].apply(score)

输出:

Cust_type
A     25.000000
B    -66.666667
C   -100.000000
D     33.333333
Name: DPP, dtype: float64

另一种方法:

d = {'Promoter': 1, 'Detractor': -1}

df['DPP'].map(d).fillna(0).groupby(df['Cust_type']).mean().mul(100)

【讨论】:

    【解决方案2】:
    df2=pd.crosstab(df1.Cust_type,df1.DPP,normalize='index')
    df2.Promoter.sub(df2.Detractor).mul(100)
    
    out:
    
    Cust_type
    A     25.000000
    B    -66.666667
    C   -100.000000
    D     33.333333
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-06
      • 1970-01-01
      • 2012-03-07
      • 2021-03-01
      • 2023-03-24
      • 2019-03-01
      • 2013-01-09
      相关资源
      最近更新 更多