I'm trying to get the number of rows of dataframe df with Pandas, and here is my code.
total_rows = df.count
print total_rows +1
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?
You can use the .shape
property or just len(DataFrame.index)
. However, there are notable performance differences ( len(DataFrame.index)
is fastest).
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 NaN
s,