我正在尝试对来自不同发行商的每部电影进行总计,将这些总计转换为所有发行商总计总数的百分比。然后,我需要将所有低于 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/