1

data frame

I hope the data is visible. It is a small textual data frame with row and columns. I need to count the occurrences of a specific word like "price" in each row of a column named "content".

zapoto
  • 11
  • 2
  • Please don't post images of code, data, or Tracebacks. [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – wwii May 18 '22 at 21:30
  • Welcome to SO. We expect some minimal effort to solve the problem and an explanation of how the solution is deficient. It is best if you provide minimal examples of the data and the expected result(s) along with your written descriptions. Please read [ask], [mre], and the other links found on those SO help pages. – wwii May 18 '22 at 21:33
  • Pandas has wonderful documentation: [https://pandas.pydata.org/docs/user_guide/index.html](https://pandas.pydata.org/docs/user_guide/index.html) – wwii May 18 '22 at 21:34

1 Answers1

1

First, let me help you display the dataframe:

date title content
10/10/2010 one thing this price is a low price
10/9/2010 two things price is high
10/8/2010 three things high price is higher price

You can write a python function to get the count of a specific word in the sentence of the content column.

def word_count(sentence, query_word):
    word_list = sentence.split()
    return word_list.count(query_word) 

if __name__=="__main__":
    sentence ='high price is higher price'         
    print(word_count(sentence, 'price'))

Or if you want count all words, try Counter

from typing import Counter

sentence ='high price is higher price'
c = Counter(sentence.split())
print(c['price'])
print(c.most_common())
lijqhs
  • 106
  • 3
  • Thank you so much @liijqhs for helping. This looks great. Next time I will make sure to display the table as you did. Ideally, I am trying to collect the output into a new column. And each row of the new column would have a count of the occurrences of the word _price_ – zapoto May 19 '22 at 06:00