Analytics Python

Word Count in Python

This article is all about word count in python. In our last article, I explained word count in PIG but there are some limitations when dealing with files in PIG and we may need to write UDFs for that.

Those can be cleared in Python. I will show you how to do a word count in Python file easily. This is a simple program which you can get done on any Python editors.

Word Count in Python

Word count in Python

Considering you have already installed Python on your system and you have a sample file on which you want to do a word count in python.

If you don’t have any sample file, recommend you to download the below file. We are using this for example purpose.

Sample File Download

Python word count example

First, open the file and save it in a variable like below-

And now the logic for word count in python will be like, we will check if the word exists in the file, just increase the count else leave it as it is.

So below is the finalized python word count code which you can directly run on your Python Editor. Just change the path of the file.

This was all about word count in python and python word count code. Hope this will help you. You will be getting output like below-

Now suppose you have to find the top 5 record from this list of words. So what will you do?

Let’s see how to find top 5 words in python?

Top 5 Words in a file in Python

Already in the above section, we have found the count of each word and now just we have to find the most 5 occurred words.

All you to do is just arrange the result of the first section in descending order so that we can find the desired result. Here is the updated code-

If you want to, even more, customize this code then here it is-

And you are done. This was all about word count in python and finding top 5 words in a file through python.

Do try these and let us know how it worked. Do share the issue, if you will experience any.

 

2 Comments

Leave a Comment