如何获得 pandas 数据帧的行数? [英] how to get row count of pandas dataframe?
问题描述
我正在尝试用Pandas获取数据帧数df,这里是我的代码
I'm trying to get the number of rows of dataframe df with Pandas, and here is my code
方法1:
total_rows = df.count
print total_rows +1
方法2:
total_rows = df['First_columnn_label'].count
print total_rows +1
这两个代码段都给我这个错误:
both the code snippets give me this error:
TypeError:不支持的+:'instancemethod'和'int'的操作数类型
TypeError: unsupported operand type(s) for +: 'instancemethod' and 'int'
我做错了什么?
编辑:根据 @root 提供的a / 15943975/4230591>答案最好的(最快的)检查df的方式长度要调用:
EDIT: According to the answer given by @root the best (the fastest) way to check df length is to call:
len(df.index)
推荐答案
您可以使用 .shape
正确或者只是 len(DataFrame.index)
,因为有显着的性能差异:
You can use the .shape
property or just len(DataFrame.index)
as there are notable performance differences:
In [1]: import numpy as np
In [2]: import pandas as pd
In [3]: df = pd.DataFrame(np.arange(9).reshape(3,3))
In [4]: df
Out[4]:
0 1 2
0 0 1 2
1 3 4 5
2 6 7 8
In [5]: df.shape
Out[5]: (3, 3)
In [6]: timeit df.shape
1000000 loops, best of 3: 1.17 us per loop
In [7]: timeit df[0].count()
10000 loops, best of 3: 56 us per loop
In [8]: len(df.index)
Out[8]: 3
In [9]: timeit len(df.index)
1000000 loops, best of 3: 381 ns per loop
编辑:作为@Dan Allen在评论 len(df.index)
和 df [0] .count()
不可互换为计数
排除 NaN
s,
As @Dan Allen noted in the comments len(df.index)
and df[0].count()
are not interchangeable as count
excludes NaN
s,
这篇关于如何获得 pandas 数据帧的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!