2013-12-19 135 views
11

python的pandas是整齊的。我試圖用熊貓數據框來替換字典列表。但是,我想知道有一種方法可以在for循環中逐行更改值,就像一樣簡單?逐行編輯熊貓數據幀

這裏的非大熊貓字典版本:

trialList = [ 
    {'no':1, 'condition':2, 'response':''}, 
    {'no':2, 'condition':1, 'response':''}, 
    {'no':3, 'condition':1, 'response':''} 
] # ... and so on 

for trial in trialList: 
    # Do something and collect response 
    trial['response'] = 'the answer!' 

...現在trialList包含更新的值,因爲trial指回到那個。非常便利!但是,列表的列表非常不方便,特別是因爲我希望能夠計算大熊貓擅長的列方式的東西。

所以從上面給出trialList,不過,我覺得我可以使它甚至做一些熊貓樣更好:

import pandas as pd  
dfTrials = pd.DataFrame(trialList) # makes a nice 3-column dataframe with 3 rows 

for trial in dfTrials.iterrows(): 
    # do something and collect response 
    trials[1]['response'] = 'the answer!' 

...但trialList保持不變在這裏。有沒有一種簡單的方法來逐行更新值,也許等同於字典版本?重要的是,它是逐行的,因爲這是一個實驗,參與者接受大量試驗並在每次試驗中收集各種數據。

回答

30

如果你真的想行由行OPS,你可以使用iterrowsloc

>>> for i, trial in dfTrials.iterrows(): 
...  dfTrials.loc[i, "response"] = "answer {}".format(trial["no"]) 
...  
>>> dfTrials 
    condition no response 
0   2 1 answer 1 
1   1 2 answer 2 
2   1 3 answer 3 

[3 rows x 3 columns] 

更好,雖然是當你可以向量化:

>>> dfTrials["response 2"] = dfTrials["condition"] + dfTrials["no"] 
>>> dfTrials 
    condition no response response 2 
0   2 1 answer 1   3 
1   1 2 answer 2   3 
2   1 3 answer 3   4 

[3 rows x 4 columns] 

而且總是有apply

>>> def f(row): 
...  return "c{}n{}".format(row["condition"], row["no"]) 
... 
>>> dfTrials["r3"] = dfTrials.apply(f, axis=1) 
>>> dfTrials 
    condition no response response 2 r3 
0   2 1 answer 1   3 c2n1 
1   1 2 answer 2   3 c1n2 
2   1 3 answer 3   4 c1n3 

[3 rows x 5 columns] 
+1

謝謝!最重要的是我需要的。不如我想要的那麼美觀/簡單,但它有效。 –

+0

@Jonas問題在於矢量化解決方案永遠是最快的。逐行迭代是**慢**(通常可以避免)。 –

+0

在我的情況下,數據是通過試驗更新試驗的,因爲受試者需要在收集所有數據之前進行「在線」實驗和分析。因此,不可能一次對所有行進行操作。 –