0

I got a Dataframe with thousands row of data and I'm analysing one of these column. There are three values presents for the elements' color: Black, Gray and Cinnamon. My work is finding the total element of each color. I found a turtorial and it uses the codes below:

gray_squirrel_count = len(data[data[Color] == "Gray"])
black_squirrel_count = len(data[data[Color] == "Black"])
cinamon_squirrel_count = len(data[data[Color] == "Cinamon"])

The codes worked but what if I got a thousands Colors in my dataframe? I don't want to write thounsands line of codes for this. I tried making a set of colors and counting how many elements have the corresponding color so I did the codes:

fur = data["Primary Fur Color"]
fur_list = set(fur.to_list())

But there are some null values in my Dataframe so my set has nan value in it. How to ignore all the null values and how to count how many elements have the corresponding color? Thanks

Avry G.
  • 39
  • 5
  • 3
    [pd.Series.value_counts](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.value_counts.html#pandas.Series.value_counts) – BeRT2me Sep 22 '22 at 04:02
  • 2
    `data["Primary Fur Color"].value_counts()` – mozway Sep 22 '22 at 04:08

0 Answers0