How to count the value of column by field name from csv file using Python and store it into another csv file

Viewed 26

Suppose I have column named "F_name" and its value is 'Yes' and 'No', I want to count how many 'Yes' and 'No' are available in that column and store the count into another CSV file

1 Answers

You can use Pandas to do that.

import pandas as pd

#Open the CSV containing the "F_name" column
df = pd.read_csv("file_name.csv")

print(df)
$   F_name
$ 0    Yes
$ 1    Yes
$ 2     No

#Count the values
df_count = df["F_name"].value_counts()

print(df_count)
$ Yes    2
$ No     1
$ Name: F_name, dtype: int64

#Save the count dataframe to a CSV
df_count.to_csv("count.csv")
Related