Which pandas expression returns the number of rows in a DataFrame df?
Correct answer
A. len(df)
16 views
A. len(df)
16 views
len(df) returns the number of rows because Python's length protocol is mapped to the frame's first axis. df.size instead multiplies rows by columns and gives the total number of cells, while df.ndim is 2 for any DataFrame. The explicit alternatives are df.shape[0] and the index's length, which agree unless the index contains duplicate labels.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is A? Ask here and get an explanation.