2016-07-01 147 views
0
stores = [[232, '2016-02-05 04:30:00', 'Test User', 1], 
      [332, '2016-02-06 04:30:00', 'Test User', 2], 
      [432, '2016-02-07 04:30:00', 'Test User', 3], 
      [532, '2016-02-08 04:30:00', 'Test User', 4], 
      [632, '2016-02-09 04:30:00', 'Test User', 5]] 

visits = pd.DataFrame(data=stores, columns=['store', 'visit', 'auditor', 'scene']) 
visits.set_index(['store', 'visit'], inplace=True) 
scenes = [[1, 1551, 2], 
      [5, 1661, 4]] 

scenes = pd.DataFrame(data=scenes, columns=['scene', 'product', 'amount']) 
scenes.set_index('scene', inplace=True) 

store_with_products = pd.merge(visits, scenes, left_on='scene', right_index=True, how='right') 

,我得到以下長相連接的結果:權與大熊貓

       auditor scene product amount 
store visit             
232 2016-02-05 04:30:00 Test User  1  1551  2 
632 2016-02-09 04:30:00 Test User  5  1661  4 

但我做right join爲什麼我不會取得相關數據與場景矩陣失蹤滿倉矩陣NAN填補? 我如何解決上面的問題?

+0

你想要的結果是什麼? –

回答

0

你想要一個左連接,而不是右連接。然後它的工作原理:

       auditor scene product amount 
store visit             
232 2016-02-05 04:30:00 Test User  1 1551.0  2.0 
332 2016-02-06 04:30:00 Test User  2  NaN  NaN 
432 2016-02-07 04:30:00 Test User  3  NaN  NaN 
532 2016-02-08 04:30:00 Test User  4  NaN  NaN 
632 2016-02-09 04:30:00 Test User  5 1661.0  4.0