Home > Back-end >  python dataframe count word occurrences
python dataframe count word occurrences

Time:05-30

I have searched a lot here and I couldnt find the answer for it. I have a dataframe with column "Descriptions" which contain a long string, I'm trying to count the number of occurence for a specific word "restaurant",

df['has_restaurants'] = 0
for index,text in enumerate(df['Description']):
    text = text.split()
    df['has_restaurants'][index] = (sum(map(lambda count : 1 if 'restaurant' in count else 0, text)))

Did the above and it works but it doesn't look like a good way to do it and it generates this "error" as well:

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df['has_restaurants'][index] = (sum(map(lambda count : 1 if 'restaurant' in count else 0, text)))

CodePudding user response:

You might simplify that by using .str.count method, consider following simple example

import pandas as pd
df = pd.DataFrame({"description":["ABC DEF GHI","ABC ABC ABC","XYZ XYZ XYZ"]})
df['ABC_count'] = df.description.str.count("ABC")
print(df)

output

   description  ABC_count
0  ABC DEF GHI          1
1  ABC ABC ABC          3
2  XYZ XYZ XYZ          0

CodePudding user response:

You could use Python's native .count() method:

df['has_restaurants'] = 0
for index,text in enumerate(df['Description']):
    df['has_restaurants'][index] = text.count('restaurant')
  • Related