Question:easy

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

Show Hint

axis=1 works across columns for each row, and count() counts non-NaN values.
Updated On: Oct 1, 2026
  • The total number of row in a dataframe.
  • The total number of values in each row.
  • The total number of columns in each dataframe
  • The total number of NaN values in each row
Show Solution

The Correct Option is B

Solution and Explanation

Step 1: Idea:
We can reason from what the result looks like. Say a DataFrame has R rows and C columns. We ask what shape of answer each option would give, and compare it with how axis=1 behaves.

Step 2: Meaning of axis = 1.
In pandas, axis=1 means the operation moves along the columns for each row. So the output has R entries, one for each row.

Step 3: Meaning of count.
count adds up how many cells are not missing. If a row has 5 cells and 2 are NaN, the count for that row is 3.

Step 4: Join the two ideas.
For each row, we get the number of non-missing values in that row. Writing it as a formula for row $i$: \[ \text{result}_i = \sum_{j=1}^{C} \mathbf{1}[\,df_{ij} \text{ is not NaN}\,] \]

Step 5: Compare with the options.
Option 1 and option 3 are single numbers, but our result has one entry per row. They do not fit. Option 4 counts NaN cells, but our formula counts non-NaN cells. They do not fit. Option 2 matches the formula.

Step 6: Extra check.
We can also compare with count() without an axis. With axis=0, which is the default, the result has one number for each column. Changing the axis to 1 flips the direction, so the result has one number for each row. A way to remember is that axis=0 collapses the rows and axis=1 collapses the columns.

Step 7: Conclusion.
Option 2 is correct. \[ \boxed{\text{The total number of values in each row}} \]
Was this answer helpful?
0


Questions Asked in CUET (UG) exam