如何获取熊猫数据帧的行计数?
如何获取熊猫数据帧的行数?df
对于数据帧,可以使用以下任何一种:df
len(df.index)
df.shape[0]
df[df.columns[0]].count()
(== 第一列中非 NaN 值的数量)重现情节的代码:
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",
)
假设是你的数据帧,那么:df
count_row = df.shape[0] # Gives number of rows
count_col = df.shape[1] # Gives number of columns
或者,更简洁地说,
r, c = df.shape