Skip to content Skip to sidebar Skip to footer

Joining Table/dataframes With Common Column In Python

I have two DataFrames: df1 = ['Date_Time', 'Temp_1', 'Latitude', 'N_S', 'Longitude', 'E_W'] df2 = ['Date_Time', 'Year', 'Month', 'Day', 'Hour',

Solution 1:

You are looking for a merge:

df1.merge(df2, on='Date_Time')

The keywords are the same as for join, but join uses only the index, see "Database-style DataFrame joining/merging".

Here's a simple example:

import pandas as pd
df1 = pd.DataFrame([[1, 2, 3]])
df2 = pd.DataFrame([[1, 7, 8],[4, 9, 9]], columns=[0, 3, 4])

In [4]: df1
Out[4]: 
   0120123

In [5]: df2
Out[5]: 
   03401781499

In [6]: df1.merge(df2, on=0)
Out[6]: 
   01234012378

In [7]: df1.merge(df2, on=0, how='outer')
Out[7]: 
   0123401237814 NaN NaN  99

If you try and join on a column you get an error:

In [8]: df1.join(df2, on=0)
# error!
Exception: columns overlap: array([0], dtype=int64)

See "Joining key columns on an index".

Post a Comment for "Joining Table/dataframes With Common Column In Python"