You can use the .shape
property or just len(DataFrame.index)
.
(您可以使用.shape
属性,也可以仅使用len(DataFrame.index)
。)
However, there are notable performance differences ( len(DataFrame.index)
is fastest): (但是,存在明显的性能差异( len(DataFrame.index)
是最快的):)
In [1]: import numpy as np
In [2]: import pandas as pd
In [3]: df = pd.DataFrame(np.arange(12).reshape(4,3))
In [4]: df
Out[4]:
0 1 2
0 0 1 2
1 3 4 5
2 6 7 8
3 9 10 11
In [5]: df.shape
Out[5]: (4, 3)
In [6]: timeit df.shape
2.77 μs ± 644 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [7]: timeit df[0].count()
348 μs ± 1.31 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [8]: len(df.index)
Out[8]: 4
In [9]: timeit len(df.index)
990 ns ± 4.97 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
EDIT: As @Dan Allen noted in the comments len(df.index)
and df[0].count()
are not interchangeable as count
excludes NaN
s,
(编辑:正如@Dan Allen在评论中指出len(df.index)
和df[0].count()
不可互换,因为count
排除了NaN
,)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…