用Python中的公共列连接表/数据aframes

时间:2021-04-28 22:59:35

I have two DataFrames:

我有两个DataFrames:

df1 = ['Date_Time',
    'Temp_1',
    'Latitude',
    'N_S',
    'Longitude',
    'E_W']

df2 = ['Date_Time',
    'Year',
    'Month',
    'Day',
    'Hour',
    'Minute',
    'Seconds']

As You can see both DataFrames have Date_Time as a common column. I want to Join these two DataFrames by matching Date_Time.

可以看到,两个DataFrames都将Date_Time作为公共列。我想通过匹配Date_Time加入这两个DataFrames。

My current code is: df.join(df2, on='Date_Time'), but this is giving an error.

我目前的代码是:df。join(df2, on='Date_Time'),但是这会产生错误。

1 个解决方案

#1


20  

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".

关键字与join相同,但是join只使用索引,参见“数据库风格的DataFrame join / merge”。

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".

参见“在索引中加入关键列”。

#1


20  

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".

关键字与join相同,但是join只使用索引,参见“数据库风格的DataFrame join / merge”。

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".

参见“在索引中加入关键列”。