Tuesday, 1 October 2013

Overriding a pandas DataFrame column with dictionary values, where the dictionary keys match a non-index column?

Overriding a pandas DataFrame column with dictionary values, where the
dictionary keys match a non-index column?

I have a DataFrame df, and a dict d, like so:
>>> df
a b
0 5 10
1 6 11
2 7 12
3 8 13
4 9 14
>>> d = {6: 22, 8: 26}
For every key/val pair in the dictionary, I'd like to find the row where
column a matches the key, and override its b column with the value. For
example, in this particular case, the value of b in row 1 will change to
22, and its value on row 3 will change to 26.
How should I do that?

No comments:

Post a Comment