2

I have following dataframe in pandas

 C1      C2       C3
 10      a        b
 10      a        b
 ?       c        c
 ?       ?        b
 10      a        b
 10      ?        ?

I want to count the occurrences of ? in all the columns

My desired output is column wise sum of occurrences

Neil
  • 7,937
  • 22
  • 87
  • 145
  • Possible duplicate of [How to get value counts for multiple columns at once in Pandas DataFrame?](https://stackoverflow.com/questions/32589829/how-to-get-value-counts-for-multiple-columns-at-once-in-pandas-dataframe) – busybear Feb 15 '19 at 17:23
  • Might help to make it easy for people and provide the full dataframe definition. – Tammo Heeren Feb 15 '19 at 17:25
  • @busybear I do not want count of all unique occurrences in the dataframe. I want specific `?` occurrences in all columns. – Neil Feb 15 '19 at 17:25
  • You can extract the counts for just `?` though – busybear Feb 15 '19 at 17:30

1 Answers1

4

Use:

m=df.eq('?').sum()
pd.DataFrame([m.values],columns=m.index)

   C1  C2  C3
0   2   2   1

Or better :

df.eq('?').sum().to_frame().T #thanks @user3483203

C1  C2  C3
0   2   2   1
anky
  • 74,114
  • 11
  • 41
  • 70