Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two DataFrames into single matching the column values

Two DataFrames have matching values stored in their corresponding 'names' and 'flights' columns. While the first DataFrame stores the distances the other stores the dates:

import pandas as pd   

distances = {'names': ['A', 'B','C'] ,'distances':[100, 200, 300]}
dates = {'flights': ['C', 'B', 'A'] ,'dates':['1/1/16', '1/2/16', '1/3/16']}

distancesDF = pd.DataFrame(distances)
datesDF = pd.DataFrame(dates)

distancesDF:

   distances    names
0        100        A
1        200        B
2        300        C

datesDF:

    dates  flights
0  1/1/16        A
1  1/2/16        B
2  1/3/16        C

I would like to merge them into single Dataframe in a such a way that the matching entities are synced with the corresponding distances and dates. So the resulted DataFame would look like this:

resultDF:

   distances    names     dates 
0        100        A    1/1/16 
1        200        B    1/2/16 
2        300        C    1/3/16

What would be the way of accomplishing it?

like image 940
alphanumeric Avatar asked Oct 29 '22 18:10

alphanumeric


1 Answers

There is nothing that ties these dataframes together other than the positional index. You can accomplish your desired example output with pd.concat

pd.concat([distancesDF, datesDF.dates], axis=1)

enter image description here


To address the edit and @kartik's comment

if we create the dfs to match what's displayed.

distances = {'names': ['A', 'B','C'] ,'distances':[100, 200, 300]}
dates = {'flights': ['A', 'B', 'C'] ,'dates':['1/1/16', '1/2/16', '1/3/16']}

distancesDF = pd.DataFrame(distances)
datesDF = pd.DataFrame(dates)

then the following two options produce the same and probably desired result.

merge

 distancesDF.merge(datesDF, left_on='names', right_on='flights')[['distances', 'names', 'dates']]

join

distancesDF.join(datesDF.set_index('flights'), on='names')

both produce

enter image description here

like image 51
piRSquared Avatar answered Nov 09 '22 09:11

piRSquared