2017-04-26 322 views

回答

2

所不同的是set_value返回一個對象,而賦值操作符分配值到現有DataFrame對象。

調用set_value後,你將可能有2個DataFrame對象(這並不一定意味着你將有兩個數據副本,DataFrame對象可以「參照」彼此),而賦值運算符將改變數據單個DataFrame對象。

這似乎是更快地使用的set_value,因爲它可能是對於用例進行了優化,而分配的方法將生成的數據的中間切片:

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 

set_value結果可能在這個是一個副本,但documentation是不是真的清楚(我):

返回:

幀:數據幀

如果標籤對包含,將參考調用數據幀,否則一個新的對象

相關問題