我在描述自己的问题时遇到了麻烦,因此我将直接解决它。这是一些测试数据:
import pandas as pd
df = pd.DataFrame(data={"family":["Smith","Miller","Simpson","Miller","Simpson","Smith","Miller","Simpson","Miller"],
"first_name":["Anna","Bart","Lisa","Ida","Paul","Bridget","Harry","Dustin","George"],
"shirt_color":["green","yellow","red","yellow","green","red","yellow","red","red"]})
现在,我想在原始数据框中创建一个新列,其中包含每个家庭的shirt_color份额,因此每一行都带有例如米勒家族和shirt_color yellow具有相同的0.75等值。
我尝试了几种方法,但是没有成功。
df = df.groupby("family").apply(lambda x: x.groupby("shirt_color").apply(lambda x: x.size()/familysize))
这似乎很有希望,但是如您所见,我再也无法访问上一个lambda函数中的家庭成员人数。我还尝试创建一个仅由family组成的groupby对象,并遍历数据框,按颜色分别对所有数据框进行分组,但是由于某种原因,我无法设法将数据框放回最后。
对于数据框来说,这似乎不是一件很奇特的事情,所以我敢肯定有一种简单的方法可以做到这一点,但是我没有想法。
非常感谢您的提前帮助!
答案 0 :(得分:3)
我认为,应避免使用apply
,因为这会导致效率低下的Python级循环。这是使用GroupBy
+ transform
的替代解决方案:
f = df.groupby('family')['first_name'].transform('size')
g = df.groupby(['family', 'shirt_color'])['first_name'].transform('size')
df['ratio'] = g / f
print(df)
family first_name shirt_color ratio
0 Smith Anna green 0.500000
1 Miller Bart yellow 0.750000
2 Simpson Lisa red 0.666667
3 Miller Ida yellow 0.750000
4 Simpson Paul green 0.333333
5 Smith Bridget red 0.500000
6 Miller Harry yellow 0.750000
7 Simpson Dustin red 0.666667
8 Miller George red 0.250000
答案 1 :(得分:3)
尝试:
df.groupby('family').apply(lambda g: g.groupby("shirt_color").apply(lambda x: x.size/g.size)).reset_index()
答案 2 :(得分:2)
您快到了。只需使用不同的变量名。通过同时使用x
,您将覆盖之前的变量,并且无法访问
df.groupby("family").apply(lambda s: s.groupby("shirt_color").apply(lambda x: x.size/s.size))
family shirt_color
Miller red 0.250000
yellow 0.750000
Simpson green 0.333333
red 0.666667
Smith green 0.500000
red 0.500000
dtype: float64
答案 3 :(得分:2)
使用value_counts
和merge
:
s = (df.groupby('family').shirt_color
.value_counts(normalize=True).rename('ratio').reset_index())
family shirt_color ratio
0 Miller yellow 0.750000
1 Miller red 0.250000
2 Simpson red 0.666667
3 Simpson green 0.333333
4 Smith green 0.500000
5 Smith red 0.500000
要将其放回初始DataFrame中:
df.merge(s)
family first_name shirt_color ratio
0 Smith Anna green 0.500000
1 Miller Bart yellow 0.750000
2 Miller Ida yellow 0.750000
3 Miller Harry yellow 0.750000
4 Simpson Lisa red 0.666667
5 Simpson Dustin red 0.666667
6 Simpson Paul green 0.333333
7 Smith Bridget red 0.500000
8 Miller George red 0.250000