Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find a particular word in a csv file in a particular column with pandas

I want to search for a particular word in a csv file and count how many words are there , I’m using pandas to get the particular column using usecols and using str.find to search the word , but it is just returning the whole column

def read(searchitem): 
  lst = ["author"] 
  df=pd.read_csv('data.csv',usecols=lst)
  df = df["author"].str.find(searchitem)
  print(df)
  
read('IMoRT')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try this:

df["author"].str.count(searchitem).sum()

EDIT:

As I understand it, you are using the same variable name for two very different things. It works, but is not recommended by best practices.

def read(searchitem): 
  lst = ["author"] 
  df=pd.read_csv('data.csv',usecols=lst)
  countWord = df["author"].str.count(searchitem).sum()
  print(countWord)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading