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]: 
   0  1  2
0  1  2  3

In [5]: df2
Out[5]: 
   0  3  4
0  1  7  8
1  4  9  9

In [6]: df1.merge(df2, on=0)
Out[6]: 
   0  1  2  3  4
0  1  2  3  7  8

In [7]: df1.merge(df2, on=0, how='outer')
Out[7]: 
   0   1   2  3  4
0  1   2   3  7  8
1  4 NaN NaN  9  9

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"