Home > Mobile >  evaluate last character of column in pandas dataframe
evaluate last character of column in pandas dataframe

Time:04-12

I have the following sentence in Python:

df['isTrue'] = np.where(df['colname'][-1] in ['R', 'V'], True, False)

But it is raising this error: KeyError: -1

How can I evaluate the last character of a column value?

CodePudding user response:

Check with isin,notice you do not need np.where here, check the the 2nd line

df['isTrue'] = np.where(df['colname'].str[-1].isin(['R', 'V']), True, False)
#df['isTrue'] = df['colname'].str[-1].isin(['R', 'V'])
  • Related