How to combine two DataFrames into a single column value match

Two DataFrames have corresponding values ​​stored in the corresponding columns "names" and "flights". 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 combine them into a single Dataframe so that the corresponding objects synchronize with the corresponding distances and dates. Thus, the given DataFame will 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 will be the way to solve it?

+5
source share
1 answer

Nothing links these data files except a positional index. You can execute the desired output result using pd.concat

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

enter image description here


To refer to edit comment and @kartik

if we create dfs to match what is 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 parameters give the same and, possibly, the desired result.

mergers

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

join to

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

both produce

enter image description here

+2
source

Source: https://habr.com/ru/post/1258306/


All Articles