'split a word in pandas and create new column
I have a column with random texts, I need to filter a word in it by creating a new column.
for example in this case I want to filter the word "Shanghai" to new column.
Can you help me plesase?
data={"A":"Shanghai : 101-150 (Univ ); QS : 314 (Univ ); THE : 351-400 (Univ ); Webometrics : 19 (Univ )","B":"Webometrics : 96 (Nom propre ); Shanghai Thématique","C":"Webometrics : 164 "}
df=pd.DataFrame(data.items(), columns=['Index', 'legend'])
df
Solution 1:[1]
This should allow you to use np.where and determine if a specific word is in the column
word_legend = 'Shanghai'
df['Text Found'] = np.where(df.legend.str.contains(word_legend ), 'Yes', 'No')
Solution 2:[2]
Here is one way to accomplish it
word = 'Shanghai'
df['text_found'] = df['legend'].str.contains(word)
df
Index legend text_found
0 A Shanghai : 101-150 (Univ ); QS : 314 (Univ ); ... True
1 B Webometrics : 96 (Nom propre ); Shanghai Théma... True
2 C Webometrics : 164 False
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ArchAngelPwn |
| Solution 2 | Naveed |
