Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading