如何计算多列的标称数据

How can I count the nominal data from multiple columns

假设我有如下所示的数据

df = pd.DataFrame({
'a':["Yes","No", "Yes"], 
'b':["Yes","No", "No"], 
'c':["Yes","Yes", "Yes"], 
'd':["No","Yes", "No"], 
'e':["Yes","No", "Yes"]})

我想从我的数据框中计算每个 Yes 并添加为新列。 结果如下所示。

a b c d e f
0 Yes Yes Yes No Yes 4
1 No No Yes Yes No 2
2 Yes No Yes No Yes 3

提前致谢!

你可以这样做:

df['f'] = df.eq('Yes').sum(axis=1)

输出:

      a   b   c   d   e  f
0   Yes Yes Yes  No Yes  4
1    No  No Yes Yes  No  2
2   Yes  No Yes  No Yes  3