[英]How To Find Percentage of Group's Total in Pandas
I want to find the percentage that each value takes, of its group, in a pandas dataframe. 我想在熊猫数据框中找到每个值占其组的百分比。
The code is below, but it is slow, due to passing the lambda function to the transform method. 该代码在下面,但是由于将lambda函数传递给transform方法,因此速度很慢。
Is there a way to speed it up? 有没有办法加快速度?
import pandas as pd
index = pd.MultiIndex.from_product([('a', 'b'), ('alpha', 'beta'), ('hello', 'world')], names=['i0', 'i1', 'i2'])
df = pd.DataFrame([[1, 2], [3, 4], [5, 6], [7, 8], [1, 2], [3, 4], [5, 6], [7, 8]], index=index, columns=['A', 'B'])
print(df)
sumto = lambda x: x/x.sum()
result = df['A'].groupby(level=['i0', 'i1']).transform(sumto)
print(result)
Output: 输出:
A B
i0 i1 i2
a alpha hello 1 2
world 3 4
beta hello 5 6
world 7 8
b alpha hello 1 2
world 3 4
beta hello 5 6
world 7 8
i0 i1 i2
a alpha hello 0.250000
world 0.750000
beta hello 0.416667
world 0.583333
b alpha hello 0.250000
world 0.750000
beta hello 0.416667
world 0.583333
Name: A, dtype: float64
df.A.unstack().pipe(lambda d: d.div(d.sum(1), 0)).stack()
i0 i1 i2
a alpha hello 0.250000
world 0.750000
beta hello 0.416667
world 0.583333
b alpha hello 0.250000
world 0.750000
beta hello 0.416667
world 0.583333
dtype: float64
df.A / df.groupby(['i0', 'i1']).A.transform('sum')
i0 i1 i2
a alpha hello 0.250000
world 0.750000
beta hello 0.416667
world 0.583333
b alpha hello 0.250000
world 0.750000
beta hello 0.416667
world 0.583333
Name: A, dtype: float64
f, u = pd.factorize([t[:2] for t in df.index.values])
df.A / np.bincount(f, df.A)[f]
i0 i1 i2
a alpha hello 0.250000
world 0.750000
beta hello 0.416667
world 0.583333
b alpha hello 0.250000
world 0.750000
beta hello 0.416667
world 0.583333
Name: A, dtype: float64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.