python dataframe行数_关于python:如何获取pandas DataFrame的行数?

我正在尝试用pandas获取数据帧df的行数,这是我的代码。方法1:

1

2total_rows = df.count

print total_rows +1

方法2:

1

2total_rows = df['First_columnn_label'].count

print total_rows +1

这两个代码段都给了我这个错误:

TypeError: unsupported operand type(s) for +: 'instancemethod' and 'int'

我做错什么了?

好吧,我发现,我应该调用方法而不是检查属性,所以它应该是df.count()no df.count

危险!注意,df.count()只返回每列的非NA/NAN行数。您应该使用df.shape[0],它总是正确地告诉您行数。

请注意,当数据帧为空时,df.count不会返回int(例如pd.dataframe(columns=["blue","red")。count不是0)

操作列表以及推荐的方法和每个方法的详细描述可以在这个答案中找到。

您可以使用.shape属性或仅使用len(DataFrame.index)属性。但是,有显著的性能差异(len(DataFrame.index)是最快的):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28In [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)

xoTgt.png

编辑:正如@dan allen在评论中指出的,len(df.index)和df[0].count()不能互换,因为count排除NaNs,

在交互工作中使用shape而不是len(df)有一个很好的理由:尝试不同的过滤,我经常需要知道还有多少项。通过形状,我只需在过滤后添加.shape就可以看到这一点。使用len()命令行的编辑变得更加繁琐,来回进行。

不适用于OP,但如果您只需要知道数据帧是否为空,那么df.empty是最佳选择。

我知道已经有一段时间了,但是len(df.index)不是381纳秒,或者0.381微秒,df.shape是3倍慢,1.17微秒。我错过什么了吗?@根

从我的测试中,df.shape[0]和len(df.index)给出了相同的性能。df.形状比以前快了一点。

(3,3)矩阵是不好的例子,因为它不显示形状元组的顺序。

如果您选择使用最快的shape属性,那么df.shape[0]将为您提供行数。

别忘了回答实际问题;答案是df.shape[0]而不是df.shape,它给出了一个元组,正如xaedes所说,最好选择一个NROWS的例子!= NCOLS

df.shape[0]比len(df)或len(df.columns)快多少?由于1ns(纳秒)=1000&181;s(微秒),因此1.17&181;s=1170ns,这意味着它大约比381ns慢3倍。

@如果你的转换是反向的:1μs=1000 ns。但你的观点是正确的,len(df.index)实际上更快。

更新后的答案反映了这样一个事实:len(df.index)是最快的方法。

看起来len(df)是最快的。

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

1

2count_row = df.shape[0] # gives number of row count

count_col = df.shape[1] # gives number of col count

也许会更像:count_row,count_col=df.shape?

为了更好地说明,故意避免使用快捷符号。

使用len(df)。这在熊猫0.11或者更早的时候起作用。

__len__()目前(0.12)与Returns length of index记录在案。计时信息,设置方式与根的答案相同:

1

2

3

4

5In [7]: timeit len(df.index)

1000000 loops, best of 3: 248 ns per loop

In [8]: timeit len(df)

1000000 loops, best of 3: 573 ns per loop

由于有一个额外的函数调用,它比直接调用len(df.index)要慢一些,但在大多数用例中,这不应该发挥任何作用。

len()是你的朋友,对行数的简短回答是len(df)。

或者,您可以通过df.index访问所有行,并通过df.columns,由于你可以用len(anyList)来获取列表的计数,因此你可以使用len(df.index)用于获取行数,len(df.columns)用于列数。

或者,可以使用返回行数和列数的df.shape,如果要访问行数,则只使用df.shape[0],只使用列数:df.shape[1]。

除上述答案外,使用可以使用df.axes得到具有行和列索引的元组,然后使用len()函数:

1

2total_rows=len(df.axes[0])

total_cols=len(df.axes[1])

这将返回索引对象,这些对象可能是原始对象的副本,也可能不是原始对象的副本,如果在检查完长度后丢弃了这些对象,这是浪费。除非您打算对索引执行其他操作,否则不要使用。

How do I get the row count of a Pandas DataFrame?

下面是一个表格,总结了所有不同的情况,在这些情况下,您希望计算一些东西,以及推荐的方法。

Licnr.png

安装程序

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27df = pd.DataFrame({

'A': list('aaabbccd'), 'B': ['x', 'x', np.nan, np.nan, 'x', 'x', 'x', np.nan]})

s = df['B'].copy()

df

A B

0 a x

1 a x

2 a NaN

3 b NaN

4 b x

5 c x

6 c x

7 d NaN

s

0 x

1 x

2 NaN

3 NaN

4 x

5 x

6 x

7 NaN

Name: B, dtype: object

数据帧中的计数行:len(df)、df.shape[0]或len(df.index)。

1

2

3

4

5

6

7

8len(df)

# 8

df.shape[0]

# 8

len(df.index)

# 8

比较固定时间操作的性能似乎很愚蠢,特别是当差异处于"认真,不要担心"的水平时。但这似乎是其他答案的一种趋势,所以为了完整性,我也这么做了。

在上述3种方法中,len(df.index)(如其他答案所述)是最快的。

Note

All the methods above are constant time operations as they are simple attribute lookups.

df.shape (similar to ndarray.shape) is an attribute that returns a tuple of (# Rows, # Cols). For example, df.shape returns (8,

2) for the example here.

数列行:len(s)、s.size、len(s.index)。

1

2

3

4

5

6

7

8len(s)

# 8

s.size

# 8

len(s.index)

# 8

s.size和len(s.index)在速度方面大致相同。但我推荐len(df)。

Note

size is an attribute, and it returns the number of elements (=count

of rows for any Series). DataFrames also define a size attribute which

returns the same result as df.shape[0] * df.shape[1].

数据帧中的计数列:df.shape[1]、len(df.columns)。

1

2

3

4

5df.shape[1]

# 2

len(df.columns)

# 2

与len(df.index)类似,len(df.columns)比这两种方法更快(但需要更多的字符来输入)。非NAN行数:DataFrame.count。

这是一个粗略的主题(因为它不精确地计算所有行,只计算非空值)。

对于系列,您可以使用Series.count():

1

2s.count()

# 5

调用DataFrame.count()将返回每列的非NAN计数:

1

2

3

4

5df.count()

A 8

B 5

dtype: int64

对每组(系列/数据帧)的所有行进行计数:GroupBy.size。

对于Series,使用SeriesGroupBy.size()。

1

2

3

4

5

6

7

8s.groupby(df.A).size()

A

a 3

b 2

c 2

d 1

Name: B, dtype: int64

对于DataFrames,使用DataFrameGroupBy.size()。

1

2

3

4

5

6

7

8df.groupby('A').size()

A

a 3

b 2

c 2

d 1

dtype: int64

每组只计算非NAN行(系列/数据帧):GroupBy.count。

与上面类似,但使用count(),而不是size()。注意,size()总是返回一个序列,而count()则返回一个序列或数据帧,这取决于如何调用它。

以下两个语句返回相同的内容:

1

2

3

4

5

6

7

8

9df.groupby('A')['B'].size()

df.groupby('A').size()

A

a 3

b 2

c 2

d 1

Name: B, dtype: int64

同时,对于count,我们有

1

2

3

4

5

6

7

8df.groupby('A').count()

B

A

a 2

b 1

c 2

d 0

…对整个GroupBy对象调用,v/s,

1

2

3

4

5

6

7

8df.groupby('A')['B'].count()

A

a 2

b 1

c 2

d 0

Name: B, dtype: int64

对特定列调用。原因应该是显而易见的。

行计数(使用任意一个):

1

2df.shape[0]

len(df)

这对现有的答案(尤其是这个答案)毫无帮助。

我是从R的背景来看大熊猫的,我发现大熊猫在选择行或列时更复杂。我不得不和它搏斗一段时间,然后我找到了一些方法来处理:

获取列数:

1

2

3

4

5len(df.columns)

## Here:

#df is your data.frame

#df.columns return a string, it contains column's titles of the df.

#Then,"len()" gets the length of it.

获取行数:

1len(df.index) #It's similar.

大熊猫用了一段时间后,我想我们应该和df.shape一起去。它分别返回行数和列数。

…基于扬·菲利普·盖尔克的回答。

len(df)或len(df.index)比df.shape[0]快的原因。看看代码。df.shape是一个@property,它运行两次调用len的数据帧方法。

1

2

3

4

5

6

7

8

9

10

11df.shape??

Type: property

String form:

Source:

# df.shape.fget

@property

def shape(self):

"""

Return a tuple representing the dimensionality of the DataFrame.

"""

return len(self.index), len(self.columns)

在len(df)的引擎盖下面

1

2

3

4

5

6

7

8df.__len__??

Signature: df.__len__()

Source:

def __len__(self):

"""Returns length of info axis, but here we use the index"""

return len(self.index)

File: ~/miniconda2/lib/python2.7/site-packages/pandas/core/frame.py

Type: instancemethod

len(df.index)比len(df)稍快,因为它的函数调用较少,但总是比df.shape[0]快。

df.shape以元组(行数、列数)的形式返回数据帧的形状。

您只需使用df.shape[0]或df.shape[1]分别访问行数或列数,这与访问元组的值相同。

如果要在链接操作的中间获取行计数,可以使用:

1df.pipe(len)

例子:

1

2

3

4

5row_count = (

pd.DataFrame(np.random.rand(3,4))

.reset_index()

.pipe(len)

)

如果不想在len()函数中放入长语句,这将非常有用。

你可以用len_uuuu()代替,但len_uuuu()看起来有点奇怪。

想要"pipe"这个操作似乎毫无意义,因为没有其他东西可以将它"pipe"到其中(它返回一个整数)。我更愿意count = len(df.reset_index())而不是count = df.reset_index().pipe(len)。前者只是一个没有函数调用的属性查找。

对于数据帧df,在浏览数据时使用的打印逗号格式行数:

1

2def nrow(df):

print("{:,}".format(df.shape[0]))

例子:

1

2nrow(my_df)

12,456,789

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值