2

I have the following DataFrame:

df = pd.DataFrame(index=['A','B','C'], columns=['x','y'])

     x    y
A  NaN  NaN
B  NaN  NaN
C  NaN  NaN

I need to update column 'x' based on matching the index value to the following dictionary:

my_dict = {'A': "map_1", 'B': "map_2", "c": "map_3"}

So, the end result should be;

     x     y
A  map_1  NaN
B  map_2  NaN
C  map_3  NaN

I know how to use the map function if I was comparing another column, but I need to compare the index.

1 Answer 1

1

Try this:

df['x'] = df.index.map(my_dict)

Output: Note the typo on my_dict small c instead of C.

       x    y
A  map_1  NaN
B  map_2  NaN
C    NaN  NaN
Sign up to request clarification or add additional context in comments.

1 Comment

yes, that's it, thanks. I didn't even thing of index.map

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.