How do I get the row count of a pandas DataFrame?

yemu picture yemu · Apr 11, 2013 · Viewed 2.2M times · Source

I'm trying to get the number of rows of dataframe df with Pandas, and here is my code.

Method 1:

total_rows = df.count
print total_rows +1

Method 2:

total_rows = df['First_columnn_label'].count
print total_rows +1

Both the code snippets give me this error:

TypeError: unsupported operand type(s) for +: 'instancemethod' and 'int'

What am I doing wrong?

Answer

root picture root · Apr 11, 2013

You can use the .shape property or just len(DataFrame.index). However, there are notable performance differences ( len(DataFrame.index) is fastest).

enter image description here


Code to reproduce the plot:

import numpy as np
import pandas as pd
import perfplot


perfplot.save(
    "out.png",
    setup=lambda n: pd.DataFrame(np.arange(n * 3).reshape(n, 3)),
    n_range=[2**k for k in range(25)],
    kernels=[
        lambda data: data.shape[0],
        lambda data: data[0].count(),
        lambda data: len(data.index),
    ],
    labels=["data.shape[0]", "data[0].count()", "len(data.index)"],
    xlabel="data rows"
)

EDIT: As @Dan Allen noted in the comments len(df.index) and df[0].count() are not interchangeable as count excludes NaNs,