Python 3.x 基于索引更新数据帧
我有以下数据帧:Python 3.x 基于索引更新数据帧,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,我有以下数据帧: df = pd.DataFrame(index=['A','B','C'], columns=['x','y']) x y A NaN NaN B NaN NaN C NaN NaN 我需要根据索引值与以下字典的匹配情况更新列“x”: my_dict = {'A': "map_1", 'B': "map_2", "c": "map_3"} 因此,最终结果应该是 x y A map_1 NaN B map_2 NaN C
df = pd.DataFrame(index=['A','B','C'], columns=['x','y'])
x y
A NaN NaN
B NaN NaN
C NaN NaN
我需要根据索引值与以下字典的匹配情况更新列“x”:
my_dict = {'A': "map_1", 'B': "map_2", "c": "map_3"}
因此,最终结果应该是
x y
A map_1 NaN
B map_2 NaN
C map_3 NaN
如果我在比较另一列,我知道如何使用map
函数,但我需要比较索引 试试这个:
df['x'] = df.index.map(my_dict)
输出:注意我的字典上的打字错误,小c而不是c
是的,就这样,谢谢。我甚至不知道index.map的事
x y
A map_1 NaN
B map_2 NaN
C NaN NaN