问题
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