如何与Pandas DataFrames合并获得值

发布于 2025-02-13 02:16:52 字数 1632 浏览 0 评论 0原文

我正在尝试将一条错误消息逆转工程。

在下面的代码中,我合并了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 webliteurl必须具有不同的格式。但是由于某种原因,我无法生成上面提到的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听不够的曲调 2025-02-20 02:16:52

这是不可能的 - pb网站也不是url可以表示为浮点,当然只是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的那些:您正在尝试在对象和float64列上合并。如果您希望继续使用pd.concat

This is impossible - neither PB website nor url can be represented as floats, except of course as just NaNs. In this case you can use

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')

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文