我有以下数据集:
id window Rank member
1 2 2 0
1 3 2 0
2 3 1 0
2 2 1 0
我希望当窗口== 3时使成员等于Rank。为此,我有以下命令:
df["member"]= df[df['window']==3]['Rank'][0]
但是,我想在对id进行分组的groupby语句中这样做。以下命令返回错误。这可能是一件很简单的事情,我在这里失踪了,但我无法解决它如何在上面的命令中使用groupby。非常感谢任何帮助。
df["member"]= df.groupby("id")[df[df['window']==3]['Rank'][0]]
答案 0 :(得分:2)
您可以使用pandas.DataFrame.where
-
df = pd.DataFrame({'id':[1,1,2,2],'window':[2,3,3,2],'Rank':[2,2,1,1],'member':[0,0,0,0]})
=>
Rank id member window
0 2 1 0 2
1 2 1 0 3
2 1 2 0 3
3 1 2 0 2
df['member'] = df['Rank'].where(df['window']==3, df['member'])
print(df)
=>
Rank id member window
0 2 1 0 2
1 2 1 2 3
2 1 2 1 3
3 1 2 0 2
答案 1 :(得分:1)
您可以使用numpy.where
或DataFrame.loc
:
df['member'] = np.where(df['window']==3, df['Rank'], df['member'])
print (df)
id window Rank member
0 1 2 2 0
1 1 3 2 2
2 2 3 1 1
3 2 2 1 0
df.loc[df['window']==3, 'member'] = df['Rank']
print (df)
id window Rank member
0 1 2 2 0
1 1 3 2 2
2 2 3 1 1
3 2 2 1 0