pandas.df.replace返回typeError:不可用的类型:' list'

发布于 2025-02-13 09:51:49 字数 493 浏览 0 评论 0原文

我正在尝试将一个列中的多个值转换为带有pandas的字符串值:

ort_raw['Location'] = ort_raw['Location'].replace({['1', 1]:['remote'],
                                                   ['0',0]:['in_lab']})

... where ort_raw是df'位置'是一个特定的列名称, 1作为字符串int必须为'远程',而0为Stringint必须是'in_lab'。此块返回以下错误:

TypeError: unhashable type: 'list'

感谢您的任何帮助!

I'm trying to convert multiple values within one column to a string value with pandas:

ort_raw['Location'] = ort_raw['Location'].replace({['1', 1]:['remote'],
                                                   ['0',0]:['in_lab']})

...where ort_raw is the df, 'Location' is a particular column name, and 1 as a string or int must be 'remote' while 0 as a string or int must be 'in_lab'. This chunk returns the following error:

TypeError: unhashable type: 'list'

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

泼猴你往哪里跑 2025-02-20 09:51:49

iiuc,可能为每个键使用唯一的密钥。

ort_raw['Location'] = ort_raw['Location'].replace({'1': 'remote', 1: 'remote', '0': 'in_lab', 0: 'in_lab'})

如果要压缩一点:

d = {'remote': [1, '1'], 'in_lab': [0, '0']}
ort_raw['Location'] = ort_raw['Location'].replace({k:v for v,l in d.items() for k in l})

nb。在这种特殊情况下,您也可以转换为字符串,只使用字符串键。

ort_raw['Location'] = ort_raw['Location'].replace({'1': 'remote', '0': 'in_lab'})

IIUC, use a unique key for each possibly.

ort_raw['Location'] = ort_raw['Location'].replace({'1': 'remote', 1: 'remote', '0': 'in_lab', 0: 'in_lab'})

If you want to compress a bit:

d = {'remote': [1, '1'], 'in_lab': [0, '0']}
ort_raw['Location'] = ort_raw['Location'].replace({k:v for v,l in d.items() for k in l})

NB. In this particular case, you could also convert to string and only use the string key.

ort_raw['Location'] = ort_raw['Location'].replace({'1': 'remote', '0': 'in_lab'})
审判长 2025-02-20 09:51:49

问题是词典。密钥不能列表。

您需要为整数和字符串提供单独的键。

to_replace = {'1': 'remote', 1: 'remote', '0': 'in_lab', 0: 'in_lab'}
ort_raw['Location'] = ort_raw['Location'].replace(to_replace)

The issue is with the dictionary. Keys can't be lists.

you need to have separate keys for the integers and strings.

to_replace = {'1': 'remote', 1: 'remote', '0': 'in_lab', 0: 'in_lab'}
ort_raw['Location'] = ort_raw['Location'].replace(to_replace)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文