【问题标题】:Value error when merging 2 dataframe with identical number of row合并具有相同行数的 2 个数据帧时出现值错误
【发布时间】:2020-02-02 15:54:03
【问题描述】:

我有一个这样的数据框:

+-----+-------+---------+
|  id | Time  | Name    |
+-----+-------+---------+
| 1   | 1     | John    |
+-----+-------+---------+
| 2   | 2     | David   |
+-----+-------+---------+
| 3   | 4     | Rebecca |
+-----+-------+---------+
| 4   | later | Taylor  |
+-----+-------+---------+
| 5   | later | Li      |
+-----+-------+---------+
| 6   | 8     | Maria   |
+-----+-------+---------+

我想根据 'id' 和时间与另一个表合并:

  data1=pd.merge(data1, data2,left_on=['id', 'time'],right_on=['id', 'time'], how='left')

其他表数据

+-----+-------+--------------+
|  id | Time  | Job          |
+-----+-------+--------------+
| 2   | 2     | Doctor       |
+-----+-------+--------------+
| 1   | 1     | Engineer     |
+-----+-------+--------------+
| 4   | later | Receptionist |
+-----+-------+--------------+
| 3   | 4     | Professor    |
+-----+-------+--------------+
| 5   | later | Lawyer       |
+-----+-------+--------------+
| 6   | 8     | Trainer      |
+-----+-------+--------------+


它引发了错误:

ValueError: You are trying to merge on int64 and object columns. If you wish to proceed you should use pd.concat

我尝试了什么:

data1['time']=data1['time'].astype(str)
data2['time']=data2['time'].astype(str)

没有用。我能做些什么? PS:在这个例子中 Id 不同,但在我的数据中 Id 可以是相同的,所以我需要合并 Time 和 Id

【问题讨论】:

标签: python dataframe error-handling merge


【解决方案1】:

您是否尝试过将“id”列也转换为 str 或 int?

抱歉,我没有足够的声誉来评论您的问题。

【讨论】:

  • 嗨,谢谢,我现在确实将两列都更改为 str,希望它可以工作
猜你喜欢
  • 2020-11-02
  • 1970-01-01
  • 2021-10-19
  • 2017-05-18
  • 1970-01-01
  • 2021-08-15
  • 2021-03-11
  • 1970-01-01
  • 2017-03-16
相关资源
最近更新 更多