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
Solution 1:
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
Post a Comment for "Python Pandas If Column String Contains Word Flag"