我正在尝试将一列中的多个值转换为使用熊猫的字符串值:
ort_raw['Location'] = ort_raw['Location'].replace({['1', 1]:['remote'],
['0',0]:['in_lab']})...where ort_raw是df,'Location'是特定的列名,1作为string或int必须是'remote',0作为string或int必须是'in_lab'。此块返回以下错误:
TypeError: unhashable type: 'list'谢谢你的帮助!
发布于 2022-07-06 21:15:20
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})注意:在这种情况下,还可以转换为string,并且只使用string键。
ort_raw['Location'] = ort_raw['Location'].replace({'1': 'remote', '0': 'in_lab'})发布于 2022-07-06 21:17:54
问题是字典的问题。钥匙不能是单子。
对于整数和字符串,您需要有单独的键。
to_replace = {'1': 'remote', 1: 'remote', '0': 'in_lab', 0: 'in_lab'}
ort_raw['Location'] = ort_raw['Location'].replace(to_replace)https://stackoverflow.com/questions/72889873
复制相似问题