3

I have a dataframe of survey responses with 'Yes/No' responses. I would like to create a new column/variable that is the total number of Yes responses.

enter image description here

I could replace all the 'Yes/No' to '1/0', then use:

df['total_variable'] = df.iloc[:, 16:22].sum(axis=1)

Or I could write a for loop.

But I was just wondering if there are any other efficient ways of doing this?

Jameson
  • 167
  • 2
  • 11

1 Answers1

2

Instead replacing compare by Yes by DataFrame.eq and then count Trues by sum:

df['total_variable'] = df.iloc[:, 16:22].eq('Yes').sum(axis=1)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Amazing! I had never come across DataFrame.eq before. This will be very handy for the future. Thanks! – Jameson Sep 08 '20 at 13:03