'How to count the number of times an element appered in entire dataframe?
I have a Pandas DataFrame like this:
pd.DataFrame({'names_1' : ['cat', 'dog', 'elephant'],
'names_2' : ['rat', 'cat', 'tiger'],
'names_3' : ['cow', 'monkey', 'cat']})
# Output -
names_1 names_2 names_3
0 cat rat cow
1 dog cat monkey
2 elephant tiger cat
I want to count the number of time 'cat' appeared in the entire dataset. I know about value_counts() method, but it only counts the element in a specific column. How do I count cat appearance in the entire dataset?
Edit - In my use case, I want to return a dictionary of counts of each element, like this:
{cat:3, dog:1, elephant:1, etc..}
Solution 1:[1]
You can use collections.Counter on the flatten underlying numpy array. This is ~2–100 times faster than using value_counts depending on the size of the dataframe.
from collections import Counter
dict(Counter(df.values.ravel().tolist()))
output:
{'cat': 3,
'rat': 1,
'cow': 1,
'dog': 1,
'monkey': 1,
'elephant': 1,
'tiger': 1}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mozway |
