python带括号的方法和不带括号的方法_带有和不带有括号的python pandas函数

I notice that many DataFrame functions if used without parentheses seem to behave like 'properties' e.g.

In [200]: df = DataFrame (np.random.randn (7,2))

In [201]: df.head ()

Out[201]:

0 1

0 -1.325883 0.878198

1 0.588264 -2.033421

2 -0.554993 -0.217938

3 -0.777936 2.217457

4 0.875371 1.918693

In [202]: df.head

Out[202]:

0 -1.325883 0.878198

1 0.588264 -2.033421

2 -0.554993 -0.217938

3 -0.777936 2.217457

4 0.875371 1.918693

5 0.940440 -2.279781

6 1.152370 -2.733546>

How is this done and is it good practice ?

This is with pandas 0.15.1 on linux

解决方案

They are different and not recommended, one clearly shows that it's a method and happens to output the results whilst the other shows the expected output.

Here's why you should not do this:

In [23]:

t = df.head

In [24]:

t.iloc[0]

---------------------------------------------------------------------------

AttributeError Traceback (most recent call last)

in ()

----> 1 t.iloc[0]

AttributeError: 'function' object has no attribute 'iloc'

In [25]:

t = df.head()

t.iloc[0]

Out[25]:

0 0.712635

1 0.363903

Name: 0, dtype: float64

So OK you don't use parentheses to call the method correctly and see an output that appears valid but if you took a reference to this and tried to use it, you are operating on the method rather than the slice of the df which is not what you intended.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值