Home > Blockchain >  split a word in pandas and create new column
split a word in pandas and create new column

Time:05-18

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

the output that i want

CodePudding user response:

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')

CodePudding user response:

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
  • Related