Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带字典的映射:InvalidIndexError:重新索引仅对唯一值索引对象有效_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 带字典的映射:InvalidIndexError:重新索引仅对唯一值索引对象有效

Python 带字典的映射:InvalidIndexError:重新索引仅对唯一值索引对象有效,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我正在尝试用Pandas 0.25.2的字典映射一个列。其目的是用相应的字典值替换列中任何字典键的每次出现。数据帧中不存在目标/输出列。我尝试将目标列预先设置为np.nan,但没有效果 In [252]: df.index.is_unique Out[252]: True In [253]: df.columns.is_unique Out[253]: True In [255]: 'hq_location' in df.columns Out[255]: False addresses

我正在尝试用Pandas 0.25.2的字典映射一个列。其目的是用相应的字典值替换列中任何字典键的每次出现。数据帧中不存在目标/输出列。我尝试将目标列预先设置为
np.nan
,但没有效果

In [252]: df.index.is_unique
Out[252]: True

In [253]: df.columns.is_unique
Out[253]: True

In [255]: 'hq_location' in df.columns
Out[255]: False

addresses = dict()
# .. populate the dict ..
df['hq_location'] = df['Headquarters Location'].map(addresses)

...

InvalidIndexError: Reindexing only valid with uniquely valued Index objects

这是一个有效的解决方法,但我不明白为什么有必要:
map(addresses.get,na_action='ignore')
这里的“addesss”只是“addresses”的一个输入错误,还是在代码本身中?“总部位置”列中似乎有N/A值,因此
字典
返回一个错误,
映射
捕获并抛出它自己的错误。@Aryerez是的,这是一个打字错误。确实存在价值观。根据
Series.map的文档:
当arg是字典时,字典中未包含的系列值(作为键)将转换为NaN。