本文介绍了在Python中使用公用列联接表/数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个DataFrame:
I have two DataFrames:
df1 = ['Date_Time', 'Temp_1', 'Latitude', 'N_S', 'Longitude', 'E_W'] df2 = ['Date_Time', 'Year', 'Month', 'Day', 'Hour', 'Minute', 'Seconds']因为您可以看到两个DataFrame都有Date_Time作为公共列.我想通过匹配Date_Time来加入这两个DataFrame.
As You can see both DataFrames have Date_Time as a common column. I want to Join these two DataFrames by matching Date_Time.
我当前的代码是:df.join(df2, on='Date_Time'),但这给出了错误.
My current code is: df.join(df2, on='Date_Time'), but this is giving an error.
推荐答案您正在寻找 merge :
You are looking for a merge:
df1.merge(df2, on='Date_Time')关键字与join相同,但join仅使用索引,请参见数据库样式的DataFrame合并/合并" .
The keywords are the same as for join, but join uses only the index, see "Database-style DataFrame joining/merging".
这是一个简单的例子:
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)请参见索引" .
更多推荐
在Python中使用公用列联接表/数据框
发布评论