np.where() do nothing if condition fails

前端 未结 2 616
不思量自难忘°
不思量自难忘° 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.

提交回复
热议问题