3

I uploaded an excel text file. I want to count the number of times each word occurs, for instance:

Output:

was 2
report 1
county 5
increase 2

Code:

 news = pd.read_excel('C:\\Users\\farid-PC\\Desktop\\Tester.xlsx')
 pd.set_option('display.max_colwidth', 1000)
 print(news)
 #implement word counter?

Current Output:

   Text
0  Trump will drop a bomb on North Korea
1  Building a wall on the U.S.-Mexico border will take literally years
2  Wisconsin is on pace to double the number of layoffs this year.
3  Says John McCain has done nothing to help the vets.
4  Suzanne Bonamici supports a plan that will cut choice for Medicare 

Any help will be appreciated.

4
  • 1
    It would be helpful if you showed us a few rows of your dataset (you can cut and paste news.head().to_dict() or something like that). Also, the code you provided doesn't attempt to solve the problem, it just shows how you loaded the data. Have you attempted anything so far? Commented Oct 23, 2018 at 20:09
  • Possible duplicate of python - find the occurrence of the word in a file Commented Oct 23, 2018 at 20:09
  • Show us how data looks like by posting output of df.head(5). You can use collections.Counter as well to count the frequency. Commented Oct 23, 2018 at 20:12
  • I tried to use news.read() but gives an error that dataframe doesn't have attribute read. Commented Oct 23, 2018 at 20:17

1 Answer 1

6

With pandas, using split, stack and value_counts:

series = df.Text.str.split(expand=True).stack().value_counts()

A python-based alternative using chain.from_iterable (to flatten) and Counter (to count):

from collections import Counter
from itertools import chain

counter = Counter(chain.from_iterable(map(str.split, df.Text.tolist()))) 

Re-create a Series of counts using:

series = pd.Series(counter).sort_values(ascending=False)

Which is identical to the pandas solution above and should be much faster since there is no stacking involved (stack is a slow operation).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help.

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.