pandas从其他2列中删除列值

时间:2018-05-31 18:27:57

标签: python python-3.x pandas dataframe

我有以下数据框

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列中,那么conditionteam列中的team2以外的值应该是被选中。大熊猫最好的方法是什么?

2 个答案:

答案 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