【发布时间】:2017-12-29 17:38:06
【问题描述】:
首先,该解决方案在我的代码pandas merge on date column issue 中不起作用
我有两个来自 mysql 查询结果的数据框,它们都有 'captureDate' 列。在 mysql 表中,数据类型为“日期”。在数据框中,数据类型为对象。
df1['captureDate'] 数据
0 2017-06-28
1 2017-06-28
2 2017-06-28
3 2017-06-28
4 2017-06-28
5 2017-06-28
6 2017-06-28
Name: captureDate, dtype: object
df2['captureDate'] 数据
0 2017-06-28
1 2017-06-28
2 2017-06-28
3 2017-06-28
4 2017-06-28
5 2017-06-28
6 2017-06-28
Name: captureDate, dtype: object
当我比较 df1 和 df2 的列时,它返回 True
print df1['captureDate'].equals(df2['captureDate'])
我的合并代码
inner = pd.merge(df1, df2, on='captureDate', how='inner')
但是,结果是错误的,它返回了 49 行。内情爆棚:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 49 entries, 0 to 48
Data columns (total 20 columns):
rule_id_x 49 non-null int64
monitor_sites_x 49 non-null object
rule_type_x 49 non-null int64
lower_limit_x 49 non-null int64
upper_limit_x 49 non-null int64
actual_x 49 non-null int64
predict_x 49 non-null int64
captureDate 49 non-null object
deviation_x 49 non-null float32
create_time_x 49 non-null int64
actual_y 49 non-null int64
create_time_y 49 non-null int64
deviation_y 49 non-null object
id 49 non-null int64
lower_limit_y 49 non-null int64
monitor_sites_y 49 non-null object
predict_y 49 non-null int64
rule_id_y 49 non-null object
rule_type_y 49 non-null int64
upper_limit_y 49 non-null int64
那么,为什么会发生这种情况以及如何处理这个问题?
【问题讨论】:
-
重复有问题。最简单的解决方案是删除重复项。有可能吗?
标签: mysql python-2.7 pandas datetime merge