python - 取经销商的总销售额,并得到每个经销商占所有经销商总销售额的百分比

标签 python pandas csv

我正在尝试对来自不同发行商的每部电影进行总计,将这些总计转换为所有发行商总计总数的百分比。然后,我需要将所有低于 1% 的分销商合并到一个名为“其他”的不同分销商中。

有 100 多个分销商,计算总销售额并为每个分销创建百分比而不是销售数量。 这是以下代码的输出。

print(df.groupby(df['Distributor'])['Tickets Sold'].sum())

Distributor
20th Century Fox                    141367982
25th Frame                               2989
26 Aries                                  867
A24                                   6494901
Abramorama Films                       367311
Anchor Bay Entertainment                12710
Archstone Entertainment                  1299
Area 23a                                 4615
ArtAffects                              48549
ArtMattan Productions                     319

最佳答案

通过比较创建 bool 掩码 sum Series.lt 对于 < , 按 boolean indexing 倒掩码过滤并通过 setting with enlargement 添加新值和 sum 1% 下过滤的行数:

mask = df.div(df.sum()).lt(0.01)
out = df[~mask]
out.loc['others'] = df[mask].sum()
print (out)
20th Century Fox    141367982
A24                   6494901
others                 438659
dtype: int64

关于python - 取经销商的总销售额,并得到每个经销商占所有经销商总销售额的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55931322/

相关文章:

python - Spotify 网络播放器 : Select Element 'Play' Button w/Selenium

python - 如何从 Pandas 的地址中提取公寓

python - 遍历列以将每个列与 Python 中的特定列进行比较

javascript - Mongoose - 版本错误 : No matching document found for id

python - 如何在 python pandas 中编辑基于另一个值的值?

python - 在 python 中进行移位排列

python - 在 Python 中解析大型 XML 文件

python 、 Pandas : join dataframes on timestamp and offset

python - 过滤日期在多个给定日期的 +/-30 天内的数据

csv - 如何将包含 boolean 值的 CSV 导入 Neo4j