Home > Blockchain >  How to search for a term in a Dataframe and return Yes or no
How to search for a term in a Dataframe and return Yes or no

Time:04-15

I have written that extracts a data from a API.That data is called cadata. I wanted to check whether a particular text contains in this column - cadata['Source'] This column is of 2000 Rows. So what i wanted to do is that tell whether that text contains in that column row or not. If yes return yes if no then return no

CodePudding user response:

I assume that you want add a column to the dataframe, containing the return for each row.

Use contains from string functions to look for a text fragment in each row. This return either True False. With replace you can map the result to yes or no.

cdata['contains_text'] = cdata['source'].str.contains(text_to_check)

# map True to 'yes' and False to 'no'
cdata['contains_text'] = cdata['contains_text'].replace({True: 'yes', False: 'no})
  • Related