jeangelj
jeangelj

Reputation: 4498

python pandas if column string contains word flag

I want to add a flag to my python pandas dataframe df, if an entry in the column Titlecontains the word test (upper or lower case or all Uppercase), I would like to add T in a new column test.

This gives me an error and does not account for all uppercase scenarios:

df['Test_Flag'] = np.where(df[df['Title'].str.contains("test|Test")==True], 'T', '')

ValueError: Length of values does not match length of index

Upvotes: 4

Views: 12676

Answers (1)

jezrael
jezrael

Reputation: 862801

You need contains with parameter case=Falseand na=False:

df['Test_Flag'] = np.where(df['Title'].str.contains("test", case=False, na=False), 'T', '')

Sample:

df = pd.DataFrame({'Title':['test','Test',np.nan, 'a']})
df['Test_Flag'] = np.where(df['Title'].str.contains("test", case=False, na=False), 'T', '')
print (df)
  Title Test_Flag
0  test         T
1  Test         T
2   NaN          
3     a  

Upvotes: 8

Related Questions