Home > Blockchain >  Counting elements in specified column of a .csv file
Counting elements in specified column of a .csv file

Time:07-21

I am programming in Python I want to count how many times each word appears in a column. Coulmn 4 of my .csv file contains cca. 7 different words and need to know how many times each one appears. Eg. there are 700 lines and I need to count how many times the phrase HelloWorld appears in column 4.

CodePudding user response:

You can use pandas.Series.value_counts() on the column you want. Since you mentioned it's the fourth column, you can get it by index using iloc as well. Of course you have to install pandas as it's not from the standard library, e.g. using pip with pip install pandas if you haven't already. An example:

import pandas as pd

df = pd.read_csv("path/to/file.csv")
forth_column = df.iloc[:, 3]  # Gets all rows for the fourth column (index starts at 0)
counts = forth_column.value_counts()
print(counts)  # You'll see the number of times each string appears in the column
# The keys are the strings and the values are the number of times they appear
hello_world_counts = counts["HelloWorld"]  
  • Related