Count rows with same values in specific column using pandas
Asked Answered
P

1

5

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?

Portative answered 10/7, 2020 at 15:27 Comment(2)
Pandas can probably be used for this. have you tried anything yet? Your question is quite general, perhaps you can add some of what you've tried so far.Nostomania
I've tried the answer to this question and it worksPortative
R
6
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.

Rickierickman answered 10/7, 2020 at 15:50 Comment(1)
Thank you very muchPortative

© 2022 - 2024 — McMap. All rights reserved.