2 回答
TA贡献1877条经验 获得超1个赞
检查transform,mean然后做np.where
s = df.groupby('A').C.transform('mean')
df['D'] = np.where(df.C>s, 'expensive', 'cheap')
df
Out[158]:
A B C D
0 car red 15 cheap
1 car blue 20 expensive
2 car grey 14 cheap
3 bike red 6 cheap
4 bike blue 8 expensive
5 phone red 9 cheap
6 phone blue 11 expensive
7 phone grey 10 cheap
TA贡献1875条经验 获得超5个赞
df['D']=np.where(df[['A', 'B', 'C']].groupby('A').apply(lambda x: (x['C'].mean()>=x['C'])),'cheap','expensive')
A B C D
0 car red 15 cheap
1 car blue 20 expensive
2 car grey 14 cheap
3 bike red 6 expensive
4 bike blue 8 cheap
5 phone red 9 cheap
6 phone blue 11 expensive
7 phone grey 10 cheap
怎么运行的
np.where(condition, if met answer, not met answer)
#Apply boolean select to get condition. In this statement we seek to return true if mean is greater than price
condition= df[['A', 'B', 'C']].groupby('A').apply(lambda x: (x['C'].mean()>=x['C']))
if met answer= 'cheap'
not me t answer='expensive'
添加回答
举报