3

I am new to python and I am trying to count non-na values, per row, and save the total to a new variable.

I have the data frame:

data = {'x1': ["Yes", "Yes", "No"],
       'x2': ["Yes",np.nan, "Yes"],
       'x3': [np.nan, np.nan, "No"]}
df = pd.DataFrame(data, columns = ['x1', 'x2', 'x3'])

print(df)
    x1   x2   x3
0  Yes  Yes  NaN
1  Yes  NaN  NaN
2   No  Yes   No

What I am trying to do is count the number of answers for each row and then save that total into a new variable. The desired output would look like this:

  x1   x2   x3   Total
0  Yes  Yes  NaN     2
1  Yes  NaN  NaN     1
2   No  Yes   No     3

It seems pretty straightforward but I cannot figure it out. Any help would be greatly appreciated.

Thank you

Jake
  • 454
  • 5
  • 26

1 Answers1

6

You just need to use count() with axis=1:

df['Total'] = df.count(axis=1)

Yields:

    x1   x2   x3  Total
0  Yes  Yes  NaN      2
1  Yes  NaN  NaN      1
2   No  Yes   No      3
rahlf23
  • 8,869
  • 4
  • 24
  • 54