Reputation: 4498
I want to add a flag to my python pandas dataframe df, if an entry in the column Title
contains 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
Reputation: 862801
You need contains
with parameter case=False
and 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