【问题标题】:dplyr groupby percentage and renaming the columndplyr groupby 百分比并重命名列
【发布时间】:2018-01-21 06:15:53
【问题描述】:

我想根据提供的促销按我的数据框分组并计算百分比。数据框格式如下

Promotion name             days rented
nan                        577
first month half off       88
nan                        22
second month free          55
nan                        60
first month half off       20

如果我的数据框被称为 df.我将如何按促销名称分组并计算天数百分比并重命名该列。因此,我的第一列将是“少于 1 个月的租金数量”。在 R 中,我会说:

df %>% group_by(`Promotion Name`) %>% 
summarise("# Rentals < 1 month" = sum(`Days rented` <= 30)/length(`Days rented`)

有人可以在 python 中提供帮助吗?我尝试了以下方法:

我希望格式为:

Promotion Name         # rentals < 1 month    # rentals < 2 month   # rentals < 3 months
None                   0.0023                 0.005                0.28
First month half off   0.78                   0.22                 0.76
2nd month free         0.44     etc

我试过了

rented_df.groupby('Promotion Name').sum()

但这并没有给我我想要的,因为我想总结

【问题讨论】:

  • 如果你要投票给我,为什么不先帮忙呢?
  • SO 不是提供代码翻译服务的网站。我想这就是你投反对票的原因。您可能会被否决的另一个原因是您没有提供cube 数据框的可重现示例。你为什么不提供数据集并发布你到目前为止尝试过的python代码?
  • 这样更好吗?

标签: python pandas python-2.7 pandas-groupby


【解决方案1】:

我认为您需要带有 boolean indexing 的自定义函数的 groupby

df = rented_df.groupby('Promotion name')['days rented']
              .apply(lambda x: x[x<=30].sum()/len(x)).reset_index(name='# Rentals < 1 month')
print (df)
         Promotion name  # Rentals < 1 month
1  first month half off            10.000000
2     second month free             0.000000

但是groupby默认删除NaNs,所以如果需要他们首先将NaN替换为fillna之前列中没有的字符串:

rented_df['Promotion name'] = rented_df['Promotion name'].fillna('NANS strings')
df = rented_df.groupby('Promotion name')['days rented']
              .apply(lambda x: x[x<=30].sum()/len(x)).reset_index(name='# Rentals < 1 month')
print (df)
         Promotion name  # Rentals < 1 month
0          NANS strings             7.333333
1  first month half off            10.000000
2     second month free             0.000000

对于单独的列需要transform:

rented_df['Promotion name'] = rented_df['Promotion name'].fillna('NANS strings')
rented_df['# Rentals < 1 month'] = rented_df.groupby('Promotion name')['days rented']
                                            .transform(lambda x: x[x<=30].sum()/len(x))
print (rented_df)
         Promotion name  days rented  # Rentals < 1 month
0          NANS strings          577             7.333333
1  first month half off           88            10.000000
2          NANS strings           22             7.333333
3     second month free           55             0.000000
4          NANS strings           60             7.333333
5  first month half off           20            10.000000

编辑:

rented_df['Promotion name'] = rented_df['Promotion name'].fillna('NANS strings')
g = rented_df.groupby('Promotion name')['days rented']
s1 = g.apply(lambda x: x[x<=30].sum()/len(x)).rename('# Rentals < 1 month')
s2 = g.apply(lambda x: x[x<=60].sum()/len(x)).rename('# Rentals < 2 month')
s3 = g.apply(lambda x: x[x<=90].sum()/len(x)).rename('# Rentals < 3 month')
df = pd.concat([s1,s2,s3], axis=1).reset_index()
print (df)
         Promotion name  # Rentals < 1 month  # Rentals < 2 month  \
0          NANS strings             7.333333            27.333333   
1  first month half off            10.000000            10.000000   
2     second month free             0.000000            55.000000   

   # Rentals < 3 month  
0            27.333333  
1            54.000000  
2            55.000000 

【讨论】:

  • 谢谢。反正有没有一次性重命名列?还有,我在哪里学习这些东西?这样我就不再问简单的问题了
  • 我现在重命名列,请参阅上次编辑。和学习的东西是最好的tutorials 我认为和代码,代码和代码。主要是需要一些时间和耐心。祝你好运!
  • 通过reset_index函数中的参数name重命名
  • 顺便说一句,如果我也想计算 x[x
  • 我认为您的最后一次编辑会覆盖未分组的rented_df 数据框。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-02
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
  • 2022-06-13
相关资源
最近更新 更多