12

I have a sample from my dataframe:

       Created      Insert Time   MatchKey              In Previous    New Type
18593  2016-08-12   2018-02-19    LXGS090393APIN040640       No        New Existing
5517   2016-08-12   2018-02-19    LIN380076CI166203726       No        New Existing
2470   2018-02-12   2018-02-19    CI164414649APIN160672      No        New Existing
13667  2016-08-12   2018-02-19    LIN257400APIN015446       Yes        New Existing
10998  2016-08-12   2018-02-19    LXSV225786APIN158860      Yes        New Existing
20149  2016-08-12   2018-02-19    LIN350167APIN158284       Yes        New Existing
20143  2016-08-12   2018-02-19    LIN350167APIN161348       Yes        New Existing
30252  2016-08-12   2018-02-19    LXGS120737APIN153339      Yes        New Existing
12583  2016-08-09   2018-02-19    WIN556410APIN157186       Yes        New Existing
28591  2018-05-03   2018-02-19    CI195705185APIN009076      No        New Created

I wanted to replace values in New Type column in a way that if the condition is failed the function does nothing:

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

but apparently it causes a syntax error as np.where() doesn't handle pass:

File "<ipython-input-9-7f68cda12cbe>", line 1
current['New Type'] = np.where(current['In Previous']=='Yes','In Previous',pass)

                                                                          ^
SyntaxError: invalid syntax

What would be an alternative to achieve the same?

jpp
  • 159,742
  • 34
  • 281
  • 339
Bartek Malysz
  • 922
  • 5
  • 14
  • 37

2 Answers2

19

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']
EdChum
  • 376,765
  • 198
  • 813
  • 562
6

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.

jpp
  • 159,742
  • 34
  • 281
  • 339