简体   繁体   中英

Count occurances of a value in an entire dataframe

For instance:

df = pd.DataFrame({A: ['Sally', 'Mark', 'John', 'Bridget', 'Anthony'], 'B': ['Tim', 'Sally', 'Sally', 'Bridget', 'Mark'], 'C': ['Matt', 'Jeff', 'Carl', 'Tim', 'Gus']})

How would I find the amount of times "Sally" appears in all of the columns combined? I'm looking for something more efficient than combining the amount of occurrences for each column.

So chain stack with value_counts

df.stack().value_counts()
Out[113]: 
Sally      3
Tim        2
Bridget    2
Mark       2
John       1
Carl       1
Matt       1
Gus        1
Jeff       1
Anthony    1
dtype: int64

Try this.

(df.values == 'Sally').sum()

However, assuming you need to do this for all the unique values, I would suggest using df.stack().value_counts() (as also suggested in @BENY's answer).

Data

import pandas as pd

df = pd.DataFrame({
    'A': ['Sally', 'Mark', 'John', 'Bridget', 'Anthony'], 
    'B': ['Tim', 'Sally', 'Sally', 'Bridget', 'Mark'], 
    'C': ['Matt', 'Jeff', 'Carl', 'Tim', 'Gus'],
})
# print(df)

#          A        B     C
# 0    Sally      Tim  Matt
# 1     Mark    Sally  Jeff
# 2     John    Sally  Carl
# 3  Bridget  Bridget   Tim
# 4  Anthony     Mark   Gus

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM