如何获得一个ValueError从合并与熊猫数据框架



我正在尝试逆向工程一个错误信息。

在下面的代码中,我合并了两个数据帧。

import pandas as pd
data = pd.DataFrame({
'PB website': ["http://www.ghi.de", "http://www.jkl.de", "http://www.def.de", "http://www.abc.de", "http://www.xyz.de"],
'PB match': [21, 22, 23, 24, 25],
'PB location': ["Süd 4", "Süd 2", "Süd 5", "Süd 3", "Süd 8"],
'PB country': ['Deutschland', 'Deutschland', 'Deutschland', 'Deutschland', 'Deutschland'],
})
processed_urls = ['http://www.abc.de', 'http://www.def.de', 'http://www.ghi.de', 'http://www.xyz.de', 'http://www.jkl.de']
flags = [False, True, True, False, True]
processed = pd.merge(left=data.loc[data['PB website'].isin(processed_urls)],
right=pd.DataFrame({'url': processed_urls, 'verlinkt': flags}),
left_on='PB website', right_on='url', how='right')
processed

结果如下:

PB website     PB match PB location  PB country         url         verlinkt
0   http://www.abc.de   24     Süd 3    Deutschland    http://www.abc.de    False
1   http://www.def.de   23     Süd 5    Deutschland    http://www.def.de    True
2   http://www.ghi.de   21     Süd 4    Deutschland    http://www.ghi.de    True
3   http://www.xyz.de   25     Süd 8    Deutschland    http://www.xyz.de    False
4   http://www.jkl.de   22     Süd 2    Deutschland    http://www.jkl.de    True

现在我想改变代码的方式,我得到以下错误消息:

ValueError:您正在尝试合并对象和float64列。如果您希望继续,您应该使用pd.concat

我知道,为了这样做,PB websiteurl必须有不同的格式。但是由于某种原因,我不能生成上面提到的ValueError

我使用的是1.4.3版的pandas

这是不可能的-PB websiteurl都不能表示为浮点数,当然NaNs除外。在这种情况下,可以使用

processed = pd.merge(left=data.loc[data['PB website'].isin(processed_urls)],
right=pd.DataFrame({'url': processed_urls, 'verlinkt': flags}).assign(url=lambda x: pd.to_numeric(x.url, 'coerce')),
left_on='PB website', right_on='url', how='right')

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

最新更新