pandas-合并字符串列不起作用(错误?)
我正在尝试在两个数据框之间进行简单合并。它们来自两个不同的SQL表,其中连接键是字符串:
>>> df1.col1.dtype
dtype('O')
>>> df2.col2.dtype
dtype('O')
我尝试使用以下方法合并它们:
>>> merge_res = pd.merge(df1, df2, left_on='col1', right_on='col2')
内部联接的结果为空,这首先提示我相交中可能没有任何条目:
>>> merge_res.shape
(0, 19)
但是当我尝试匹配单个元素时,我看到了这种非常奇怪的行为。
# Pick random element in second dataframe
>>> df2.iloc[5,:].col2
'95498208100000'
# Manually look for it in the first dataframe
>>> df1[df1.col1 == '95498208100000']
0 rows × 19 columns
# Empty, which makes sense given the above merge result
# Now look for the same value as an integer
>>> df1[df1.col1 == 95498208100000]
1 rows × 19 columns
# FINDS THE ELEMENT!?!
因此,这些列是使用’object’dtype定义的。以字符串搜索它们不会产生任何结果。将它们搜索为整数确实会返回结果,并且我认为这就是合并在上方无法正常工作的原因。
有什么想法吗?
几乎有人认为Pandas可以df1.col1
将其转换为整数,即使它在匹配 时应 被视为字符串。
(我尝试使用示例数据帧来复制此示例,但是对于较小的示例,我看不到这种行为。关于如何找到更具描述性的示例的任何建议也将不胜感激。)