【发布时间】: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