2013-10-21 63 views
2

我試圖將一系列的值添加到數據幀。我沒有試圖將該列添加到數據框的末尾,即在數據框中創建一個額外的列。將一系列的值添加到數據幀

我有一個系列,其長度與數據框中的列相同。我想將該系列中的值添加到框架中的每一列。 我試過Dataframe.add()但它沒有工作。

代碼: 數據框(稱爲讀者):

 Current track blade 1 Current track blade 2 
0    -8.577881    -0.333252 
1    -6.284912    5.294434  
2     6.269775    11.229490  
3     5.253418    10.187260  
4    -16.088380    -2.967773 

系列(abs_track命名)添加到每一列:

Current abs track 
0   3503.125 
1   3517.333 
2   3504.778 
3   3476.127 
4   3477.776 

reader=reader+abs_trackreader.add('abs_track')給予:

 Current abs track Current track blade 1 Current track blade 2  


0     NaN      NaN      NaN      
1     NaN      NaN      NaN 
2     NaN      NaN      NaN 
3     NaN      NaN      NaN 
4     NaN      NaN      NaN 

有任何想法嗎。

回答

2

您可以使用應用功能:

>>> df1 = pd.DataFrame({"Current track blade 1":[-8.577881, -6.284912, 6.269775, 5.253418, -16.088380], "Current track blade 2": [-0.333252, 5.294434, 11.229490,10.187260,-2.967773]}) 
>>> s1 = pd.Series([3503.125, 3517.333, 3504.778, 3476.127, 3477.776]) 
>>> df1.apply(lambda x: x + s1) 
    Current track blade 1 Current track blade 2 
0   3494.547119   3502.791748 
1   3511.048088   3522.627434 
2   3511.047775   3516.007490 
3   3481.380418   3486.314260 
4   3461.687620   3474.808227 
+0

道歉我捏你的設置代碼爲我的答案 – EdChum

+0

@EdChum沒問題,你的答案+1 –

5

你需要傳遞的參數axis=0達到你想要的東西:

df = pd.DataFrame({"Current track blade 1":[-8.577881, -6.284912, 6.269775, 5.253418, -16.088380], "Current track blade 2": [-0.333252, 5.294434, 11.229490,10.187260,-2.967773]}) 
s = pd.Series([3503.125, 3517.333, 3504.778, 3476.127, 3477.776]) 

df.add(s, axis=0) 

Out[58]: 
    Current track blade 1 Current track blade 2 
0   3494.547119   3502.791748 
1   3511.048088   3522.627434 
2   3511.047775   3516.007490 
3   3481.380418   3486.314260 
4   3461.687620   3474.808227 

這將增加逐列而不是按行這是默認。

相關問題