Count rows with same values in specific column using pandas

Viewed 8335

I have a csv file containing many products (40k) and many columns with price,category,name... I want to count how many rows (products) are there in each category (taking them directly from the file) and display it. How can i do this using pandas? Or is it better to use something else?

1 Answers
import pandas as pd

df = pd.read_csv(PATH_TO_CSV, usecols=['category','products'])
print(df.groupby(['category']).count())

The first line creates a dataframe with two columns(categories and products) and the second line prints out the number of products in each category.

Related