Skip to content Skip to sidebar Skip to footer

Taking Specific Elements From A Df And Putting Them Into Own Df. Python

Can someone tell me what function would tell me the number of times each string value is iterated through a Dataframe column. I tried using count but that is only returning a 1 eac

Solution 1:

I guess, we're talking about a pandas-dataframe here. So count()in combination withgroupby() would do the Job:

print(dataframe)

     animal   status0  dinosaur  extinct
1       dog   extant
2       cat   extant
3     horse   extant
4      dodo  extinct

print(dataframe.groupby(["status"])["status"].count())

status
extant     3
extinct    2
Name: status, dtype: int64

If you need more details, check the groupby-Documentation

Post a Comment for "Taking Specific Elements From A Df And Putting Them Into Own Df. Python"