Reputation: 322
I have a pandas DataFrame for which I'd like to replace one column value for a small subset of rows. E.g. given:
in_dict = [{ 'what': 'abc', 'num': 42}, {'what': 'abc', 'num': 43}, {'what': 'def', 'num': 99}]
df = pd.DataFrame.from_records(in_dict)
which looks like:
what num
0 abc 42
1 abc 43
2 def 99
Using
lookup = {'abc': 'xyz'}
I want to end up with:
what num
0 xyz 42
1 xyz 43
2 def 99
I can use map, but then I get NaN's for entries not in the lookup table:
>>> df['what'] = df['what'].map(lookup)
>>> df
what num
0 xyz 42
1 xyz 43
2 NaN 99
My actual DataFrame is about 100k items, of which <100 need the update.
What is the most efficient (and ideally pandas-idiomatic) approach here?
Upvotes: 0
Views: 53
Reputation:
You could use fillna
on the map
ped Series to fill it with the original:
df['what'] = df['what'].map(lookup).fillna(df['what'])
But a better option is to use replace
:
df['what'] = df['what'].replace(lookup)
Output:
what num
0 xyz 42
1 xyz 43
2 def 99
Upvotes: 3