0

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

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Worked perfectly! so simple, god I'm stupid. Thank you!
Don't forget to mark this as the answer to your question, so it may help others in the future
True, do you know what to do if I want to count more than one word?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.