Thursday 24 September 2020

SOLVED - Pandas Merging 101

 

Pandas Merging 101

pandas.DataFrame.merge, Otherwise if joining indexes on indexes or indexes on a column or columns, the index will be passed on. Parameters: right : DataFrame. how : {'left', 'right', 'outer',​  You can do this with merge: df_merged = df1.merge(df2, how='outer', left_index=True, right_index=True) The keyword argument how='outer' keeps all indices from both frames, filling in missing indices with NaN. The left_index and right_index keyword arguments have the merge be done on the indices.

 

 setup -

np.random.seed(0)
left = pd.DataFrame({'key': ['A', 'B', 'C', 'D'], 'value': np.random.randn(4)})    
right = pd.DataFrame({'key': ['B', 'D', 'E', 'F'], 'value': np.random.randn(4)})
  
left

  key     value
0   A  1.764052
1   B  0.400157
2   C  0.978738
3   D  2.240893

right

  key     value
0   B  1.867558
1   D -0.977278
2   E  0.950088
3   F -0.151357

 

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel