np.where() do nothing if condition fails

前端 未结 2 606
不思量自难忘°
不思量自难忘° 2021-01-06 05:48

I have a sample from my dataframe:

       Created      Insert Time   MatchKey              In Previous    New Type
18593  2016-08-12   2018-02-19    LXGS0903         


        
相关标签:
2条回答
  • 2021-01-06 06:14

    You can use pd.Series.mask for exactly this purpose:

    df['New Type'].mask(df['In Previous']=='Yes', 'In Previous', inplace=True)
    

    Somewhat confusingly, you have to remember that pd.Series.mask updates a value when a condition is met, while pd.Series.where updates a value when a condition is not met.

    0 讨论(0)
  • 2021-01-06 06:21

    Just return the column instead of pass this is the same as doing nothing when the condition is False:

    current['New Type'] = np.where(current['In Previous']=='Yes','In Previous',current['New Type'] )
    

    Or you can just mask those rows:

    current['New Type'] = current.loc[current['In Previous']=='Yes', 'In Previous']
    
    0 讨论(0)
提交回复
热议问题