Count occurrences of certain string in entire pandas dataframe

眉间皱痕 提交于 2021-02-05 06:50:32

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/54714183/count-occurrences-of-certain-string-in-entire-pandas-dataframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!