alphanumeric alphanumeric - 6 months ago 20
Python Question

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)


distances names
0 100 A
1 200 B
2 300 C


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:


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?


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