我想知道,与下面的方法相比,是否有更好的方法可以计算出熊猫在父母总数中所占的份额:非常感谢您的帮助!
raw_data = {'product': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'],
'revenue': [10,20,20,0,50,50,0,0,30]}
df = pd.DataFrame(raw_data, columns = ['product', 'revenue'])
unique_values = df['product'].unique()
L = pd.DataFrame ()
for value in unique_values:
small_df = df[df['product']==value]
small_df['shares'] = small_df['revenue']/small_df['revenue'].sum()
L = L.append(small_df, ignore_index=True)
print(L)
答案 0 :(得分:3)
尝试一下:
~/.guardrc
答案 1 :(得分:1)
(df.set_index("product") / df.groupby('product').sum()).reset_index()
product revenue
0 A 0.2
1 A 0.4
2 A 0.4
3 B 0.0
4 B 0.5
5 B 0.5
6 C 0.0
7 C 0.0
8 C 1.0
答案 2 :(得分:1)
您可以将revenue
除以GroupBy
+ transform
与sum
:
df['shares'] = df['revenue'] / df.groupby('product')['revenue'].transform('sum')
print(df)
product revenue shares
0 A 10 0.2
1 A 20 0.4
2 A 20 0.4
3 B 0 0.0
4 B 50 0.5
5 B 50 0.5
6 C 0 0.0
7 C 0 0.0
8 C 30 1.0