pandas的set_value赋值方式

The difference is that set_value is returning an object, while the assignment operator assigns the value into the existing DataFrame object.

after calling set_value you will potentially have two DataFrame objects (this does not necessarily mean you’ll have two copies of the data, as DataFrame objects can “reference” one another) while the assignment operator will change data in the single DataFrame object.

It appears to be faster to use the set_value, as it is probably optimized for that use-case, while the assignment approach will generate intermediate slices of the data:

In [1]: import pandas as pd

In [2]: import numpy as np

In [3]: df=pd.DataFrame(np.random.rand(100,100))

In [4]: %timeit df[10][10]=7
The slowest run took 6.43 times longer than the fastest. This could mean that an intermediate result is being cached
10000 loops, best of 3: 89.5 µs per loop

In [5]: %timeit df.set_value(10,10,11)
The slowest run took 10.89 times longer than the fastest. This could mean that an intermediate result is being cached
100000 loops, best of 3: 3.94 µs per loop
the result of set_value may be a copy, but the documentation is not really clear (to me) on this:

Returns:

frame : DataFrame

If label pair is contained, will be reference to calling DataFrame, otherwise a new object

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值