2017-03-28 67 views
3
>>> p1.head()   
    StreamId   Timestamp SeqNum 
0   3 1490250116391063414 1158 
1   3 1490250116391348339 3600 
2   3 1490250116391542829 3600 
3   3 1490250116391577184 1437 
4   3 1490250116392819426 1389 


>>> oss.head() 
    OrderID Symbol Stream  SeqNo 
0 5000000 AXBANK  3  1158 
1 5000001 AXBANK  6  1733 
2 5000002 AXBANK  6  1244 
3 5000003 AXBANK  6  1388 
4 5000004 AXBANK  3  1389 

這怎麼能使用2點的屬性鍵(序號(SeqNum)和流ID)多列作爲鍵地圖數據框大熊貓

>>> merge 
    OrderID Symbol Stream  SeqNo Timestamp 
0 5000000 AXBANK  3  1158  1490250116391063414 
1 5000001 AXBANK  6  1733  NaN 
2 5000002 AXBANK  6  1244  NaN 
3 5000003 AXBANK  6  1388  NaN 
4 5000004 AXBANK  3  1389  1490250116392819426 

我嘗試使用

oss['Time1'] = oss['SeqNo'].map.((p1.set_index('SeqNum')['Timestamp'])) 

合併但我需要包括兩個(SeqNum-SeqNo & Stream-StreamId)作爲鍵 我知道這可以很容易,如果我重命名在兩個數據框相同的列名稱,並使用合併,但我想避免這一點。我寧願用一些普通的像(借這個數據幀,這些列的那些列ANOTHER數據幀映射和讀取所需coulmns)

回答

2

我認爲你需要mergedrop

print (pd.merge(oss, p1, left_on=['Stream','SeqNo'], 
         right_on=['StreamId','SeqNum'],how='left') 
      .drop(['StreamId','SeqNum'], axis=1)) 

    OrderID Symbol Stream SeqNo  Timestamp 
0 5000000 AXBANK  3 1158 1.490250e+18 
1 5000001 AXBANK  6 1733   NaN 
2 5000002 AXBANK  6 1244   NaN 
3 5000003 AXBANK  6 1388   NaN 
4 5000004 AXBANK  3 1389 1.490250e+18 

另一個與rename列解:

d = {'Stream':'StreamId','SeqNo':'SeqNum'} 
print (pd.merge(oss.rename(columns=d), p1, how='left')) 
    OrderID Symbol StreamId SeqNum  Timestamp 
0 5000000 AXBANK   3 1158 1.490250e+18 
1 5000001 AXBANK   6 1733   NaN 
2 5000002 AXBANK   6 1244   NaN 
3 5000003 AXBANK   6 1388   NaN 
4 5000004 AXBANK   3 1389 1.490250e+18 
+0

有沒有辦法保持時間戳,因爲它是,而不是在e – pythonRcpp

+0

我認爲只有一種方法 - 先轉換爲'str' - 'p1.Timestamp = p1.Timestamp.astype(str)'。因爲不可能將'int' valus和'float'一起''int'總是轉換爲'float' - 參見[docs](http://pandas.pydata.org/pandas-docs/stable/gotchas.html# NA型-促銷) – jezrael

4

使用join

oss.join(p1.set_index(['StreamId', 'SeqNum']), on=['Stream', 'SeqNo']) 

    OrderID Symbol Stream SeqNo  Timestamp 
0 5000000 AXBANK  3 1158 1.490250e+18 
1 5000001 AXBANK  6 1733   NaN 
2 5000002 AXBANK  6 1244   NaN 
3 5000003 AXBANK  6 1388   NaN 
4 5000004 AXBANK  3 1389 1.490250e+18