我有以下数据框
team team2 condition Required_Col
Australia Sri Lanka Sri Lanka Australia
Australia Sri Lanka Sri Lanka Australia
Australia Sri Lanka Sri Lanka Australia
Australia Sri Lanka Australia Sri Lanka
Australia Sri Lanka Australia Sri Lanka
我想创建Required_Col
,以便如果值在condition
列中,那么condition
和team
列中的team2
以外的值应该是被选中。大熊猫最好的方法是什么?
答案 0 :(得分:4)
使用 np.where
df['required'] = np.where(df.condition == df.team, df.team2, df.team)
team team2 condition required
0 Australia SriLanka SriLanka Australia
1 Australia SriLanka SriLanka Australia
2 Australia SriLanka SriLanka Australia
3 Australia SriLanka Australia SriLanka
4 Australia SriLanka Australia SriLanka
答案 1 :(得分:1)
您可以使用.apply():
df['Required_Col'] = df.apply(lambda x: x['team2'] if (x['condition']==x['team']) else x['team'], axis=1)
输出:
team team2 condition Required_Col
0 Australia SriLanka SriLanka Australia
1 Australia SriLanka SriLanka Australia
2 Australia SriLanka SriLanka Australia
3 Australia SriLanka Australia SriLanka
4 Australia SriLanka Australia SriLanka