如何获取panda数据帧df的行数?
当前回答
假设数据集是“data”,将数据集命名为“data_fr”,data_fr中的行数为“nu_rows”
#import the data frame. Extention could be different as csv,xlsx or etc.
data_fr = pd.read_csv('data.csv')
#print the number of rows
nu_rows = data_fr.shape[0]
print(nu_rows)
其他回答
我从R背景来到Pandas,我发现Pandas在选择行或列时更加复杂。
我不得不与它搏斗一段时间,然后我找到了一些应对方法:
获取列数:
len(df.columns)
## Here:
# df is your data.frame
# df.columns returns a string. It contains column's titles of the df.
# Then, "len()" gets the length of it.
获取行数:
len(df.index) # It's similar.
这两种方法都可以做到(df是DataFrame的名称):
方法1:使用len函数:
len(df)将给出名为df的DataFrame中的行数。
方法2:使用计数函数:
df[col].count()将计算给定列col中的行数。
df.count()将给出所有列的行数。
TL;DR使用长度(df)
len()返回列表对象的项数(长度)(也适用于字典、字符串、元组或范围对象)。因此,要获取DataFrame的行计数,只需使用len(df)。有关len函数的更多信息,请参见官方页面。
或者,您可以分别使用df.index和df.columns访问所有行和所有列len(df.index)将给出行数,len(df.columns)将给出列数。
或者,您可以使用df.shape,它一起返回行数和列数(作为元组),在这里您可以使用索引访问每个项。如果要访问行数,请仅使用df.shape[0]。对于列数,请只使用:df.shape[1]。
假设数据集是“data”,将数据集命名为“data_fr”,data_fr中的行数为“nu_rows”
#import the data frame. Extention could be different as csv,xlsx or etc.
data_fr = pd.read_csv('data.csv')
#print the number of rows
nu_rows = data_fr.shape[0]
print(nu_rows)
假设df是您的数据帧,那么:
count_row = df.shape[0] # Gives number of rows
count_col = df.shape[1] # Gives number of columns
或者更简洁地说,
r, c = df.shape
推荐文章
- 使用python创建一个简单的XML文件
- APT命令行界面式的yes/no输入?
- 如何打印出状态栏和百分比?
- 在Python中获取大文件的MD5哈希值
- 在Python格式字符串中%s是什么意思?
- 如何循环通过所有但最后一项的列表?
- python用什么方法避免默认参数为空列表?
- ValueError: numpy。Ndarray大小改变,可能表示二进制不兼容。期望从C头得到88,从PyObject得到80
- Anaconda /conda -安装特定的软件包版本
- 我在哪里调用Keras的BatchNormalization函数?
- 打印测试执行时间并使用py.test锁定缓慢的测试
- 插入一行到熊猫数据框架
- 要列出Pandas DataFrame列
- 在Django模型中存储电话号码的最佳方法是什么?
- 从导入的模块中模拟函数