如何获取熊猫数据帧的行计数?

2022-09-05 00:53:12

如何获取熊猫数据帧的行数?df


答案 1

对于数据帧,可以使用以下任何一种:df

Performance 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",
)

答案 2

假设是你的数据帧,那么:df

count_row = df.shape[0]  # Gives number of rows
count_col = df.shape[1]  # Gives number of columns

或者,更简洁地说,

r, c = df.shape