我试着从不同的发行商那里获取每部电影的总数,将这些总数转化为所有发行商加起来的总数的百分比。然后,我需要将低于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
发布于 2019-05-01 12:57:48
通过比较<
的sum
和Series.lt
来创建布尔掩码,根据boolean indexing
的反向掩码过滤,并根据1%
下过滤的行的setting with enlargement和sum
添加新值
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
https://stackoverflow.com/questions/55931322
复制相似问题