snippetpythonCritical
How do I get the row count of a Pandas DataFrame?
Viewed 0 times
pandashowthegetcountrowdataframe
Problem
How do I get the number of rows of a pandas dataframe
df?Solution
For a dataframe
Code to reproduce the plot:
df, one can use any of the following:len(df.index)
df.shape[0]
df[df.columns[0]].count()(== number of non-NaN values in first column)
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 df: len(df.index),
lambda df: df.shape[0],
lambda df: df[df.columns[0]].count(),
],
labels=["len(df.index)", "df.shape[0]", "df[df.columns[0]].count()"],
xlabel="Number of rows",
)
Context
Stack Overflow Q#15943769, score: 3012
Revisions (0)
No revisions yet.