Skip to main contentSkip to solution

What will be the output of the command df.count(axis = 1)?

Solution

✅ Correct Option: 2

In pandas, df.count(axis = 1) goes across the columns and returns one number for each row. That number is how many values the row holds, and cells with NaN are not counted. With axis = 0 (the default), it gives the count for each column instead.


Option 1: the number of rows in a dataframe comes from len(df) or df.shape[0], not from count().

Option 3: count() returns a count for every row, not for each dataframe, and the number of columns comes from df.shape[1].

Option 4: this is the opposite of what count() does, since it skips NaN values. NaN counts per row come from df.isnull().sum(axis = 1).

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question