【发布时间】:2020-01-07 18:15:43
【问题描述】:
这些问题都没有解决这个问题:Question 1 和 Question 2 我也无法在 pandas 文档中找到答案。
您好,我正在尝试查找此错误的根本原因:
ValueError: You are trying to merge on object and int64 columns.
我知道我可以使用 pandas concat 或 merge 函数解决此问题,但我正在尝试了解错误的原因。问题是:为什么我会得到这个ValueError?
这是使用的两个数据帧上 head(5) 和 info() 的输出。
print(the_big_df.head(5)) 输出:
account apt apt_p balance date day flag month reps reqid year
0 AA0420 0 0.0 -578.30 2019-03-01 1 1 3 10 82f2d761 2019
1 AA0420 0 0.1 -578.30 2019-03-02 2 1 3 10 82f2d761 2019
2 AA0420 0 0.1 -578.30 2019-03-03 3 1 3 10 82f2d761 2019
3 AA0421 0 0.1 -607.30 2019-03-04 4 1 3 10 82f2d761 2019
4 AA0421 0 0.1 -610.21 2019-03-05 5 1 3 10 82f2d761 2019
print(the_big_df.info()) 输出:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 36054 entries, 0 to 36053
Data columns (total 11 columns):
account 36054 non-null object
apt 36054 non-null int64
apt_p 36054 non-null float64
balance 36054 non-null float64
date 36054 non-null datetime64[ns]
day 36054 non-null int64
flag 36054 non-null int64
month 36054 non-null int64
reps 36054 non-null int32
reqid 36054 non-null object
year 36054 non-null int64
dtypes: datetime64[ns](1), float64(2), int32(1), int64(5), object(2)
memory usage: 3.2+ MB
这是我传递给join() 的数据框; print(df_to_join.head(5)):
reqid id
0 54580f39 13301
1 3ba905c0 77114
2 5f2d80da 13302
3 a1478e98 77115
4 9b09854b 78598
print(df_to_join.info()) 输出:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 14332 entries, 0 to 14331
Data columns (total 2 columns):
reqid 14332 non-null object
dni 14332 non-null object
上述 4 次打印之后的确切下一行是:
the_max_df = the_big_df.join(df_to_join,on='reqid')
如前所述,输出是:
ValueError: You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat
为什么会发生这种情况,在明确指出列reqid 是两个数据帧中的对象之前?谢谢。
【问题讨论】:
标签: python pandas dataframe join