如何与Pandas DataFrames合并获得值
我正在尝试将一条错误消息逆转工程。
在下面的代码中,我合并了2个数据范围。
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 weblite
和url
必须具有不同的格式。但是由于某种原因,我无法生成上面提到的valueerror
。
我正在使用Pandas版本1.4.3
I am trying to reverse engineer an error message.
In the code below I merge 2 dataframes.
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
The result looks like this:
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
Now I want to change the code in a way that I get the following error message:
ValueError: You are trying to merge on object and float64 columns. If you wish to proceed you should use pd.concat
I know, in order to do so, PB website
and url
have to have different format. But for some reason I can not generate the ValueError
mentioned above.
I am using pandas version 1.4.3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是不可能的 -
pb网站
也不是url
可以表示为浮点,当然只是nans
。在这种情况下,您可以使用抛出
valueError的那些:您正在尝试在对象和float64列上合并。如果您希望继续使用pd.concat
。This is impossible - neither
PB website
norurl
can be represented as floats, except of course as justNaNs
. In this case you can usewhich throws a
ValueError: You are trying to merge on object and float64 columns. If you wish to proceed you should use pd.concat
.