samedi 28 mars 2020

How can I check a column for a sub-string and update another column if found?

I am quite new to python and my searches haven't been productive, so I hope someone could help or direct me to where it has been answered. Basically, I am trying to search a particular column for a substring and if that substring exists, update the corresponding row on a different column.

I will illustrate with this dataset

    A   B            C
    1   CYESC        THIS
    2   ABCD         0
    3   YES BOY      THIS
    4   BOY YES CAN  THIS
    5   ryrruei      0

I want to "THIS" to show up in Column C if column "B" contains the substring "YES" and "0" if it doesn't.

I tried running this code, which came out with an error message:

import pandas as pd 
import numpy as np

df['C'] = pd.np.where(df['B'].str.contains('YES'), 'THIS')

I also tried using if:

if 'YES' in [df['B']]:
     df['C'] = "THIS"
Else:
    df['C'] = 0    

Still wasn't successful. I know there is something I am missing...please any suggestion would be appreciated. Cheers

Aucun commentaire:

Enregistrer un commentaire