Combine three dataframes

122 views Asked by At

I am new to pandas dataframe and have the following question.

I have 3 dataframes coming from reading CSV files:

  • DataFrame 1 is named pdDop and has the following entries:

    DOP_WNC   DOP_TOW  DOP_NRSVS  DOP_PDOP  DOP_VDOP  DOP_HDOP  DOP_TDOP
    
    1928  424800.0          4      5.81      5.36      2.24      2.72
    
    1928  424801.0          4      5.81      5.36      2.24      2.72
    
    1928  424802.0          4      5.80      5.35      2.24      2.72
    
    1928  424803.0          4      5.80      5.35      2.24      2.72
    
    1928  424804.0          4      5.80      5.35      2.24      2.72
    
    1928  424805.0          4      5.80      5.35      2.24      2.72
    
  • DataFrame 2 is named pdGeod and has the following entries:

    GEOD_TOW  GEOD_MODE  GEOD_2D/3D  GEOD_Error  GEOD_NrSV  GEOD_Latitude  GEOD_Longitude  GEOD_Height
    
    424800.0          1           0           0          4         0.8874          0.0767     150.4975
    
    424801.0          1           0           0          4         0.8874          0.0767     150.5277
    
    424802.0          1           0           0          4         0.8874          0.0767     150.5579
    
    424803.0          1           0           0          4         0.8874          0.0767     150.5931
    
    424804.0          1           0           0          4         0.8874          0.0767     150.6214
    
  • Dataframe 3 is called pdSatVis and has following entries:

    VISIBILITY_TOW  VISIBILITY_SVID  VISIBILITY_AZIMUTH  VISIBILITY_ELEVATION
    
    426175.0               92               54.50                 35.43
    
    426175.0              100              108.22                 26.00
    
    426175.0               88               49.29                 10.48
    
    426175.0               89              278.29                 17.39
    
    426176.0               92               54.50                 35.43
    
    426176.0              100              108.22                 26.00
    
    426176.0               88               49.29                 10.48
    
    426176.0               89              278.29                 17.39
    
    426177.0               92               54.48                 35.42
    
    426177.0              100              108.23                 25.98
    
    426177.0               88               49.28                 10.45
    
    426177.0               89              278.27                 17.38
    
    426178.0               92               54.48                 35.42
    

I would like to create 1 dataframe which combines based on the *_TOW (Time Of Week) column which is in each dataframe. Remark that the last dataframe pdSatVis has several lines with the VISIBILTY_TOW value that corresponds only to 1 row in both pdDop and pdGeod.

1

There are 1 answers

0
Mike Müller On

You can either add a new column to merge on:

pdDop['TOW'] = pdDop['DOP_TOW']
pdGeod['TOW'] = pdGeod['GEOD_TOW']
pdSatVis['TOW'] = pdSatVis['VISIBILITY_TOW']
pd.merge(pd.merge(pdDop, pdGeod, how='outer'), pdSatVis, how='outer')

or provide the columns to merge on explicitly:

m1 = pd.merge(pdDop, pdGeod, how='outer', left_on='DOP_TOW', right_on='GEOD_TOW')
pd.merge(m1, pdSatVis, how='outer', left_on='DOP_TOW', right_on='VISIBILITY_TOW')