Issue
Is there a way to pass a list of columns to map a dictionary's values in a single call?
I can do it by stating each column but wondering if I can shorten up the syntax.
Looked at this post for reference.
Sample data:
import pandas as pd
# initialize data of lists.
data = {'id': ['a', 'b', 'c', 'd', 'e'],
'col1': ['PHX', 'BKN', 'X', 'PHX', 'X'],
'col2': ['X', 'PHX', 'BKN', 'BKN', 'X'],
'col3': ['PHX', 'BKN', 'PHX', 'BKN', 'PHX']
}
df = pd.DataFrame(data)
df
id col1 col2 col3
0 a PHX X PHX
1 b BKN PHX BKN
2 c X BKN PHX
3 d PHX BKN BKN
4 e X X PHX
I want to apply this mapping to col1
and col2
but not col3
:
name_dict = {'PHX' : 'PHO', 'BKN': 'NJN'}
So PHX -> PHO
and BKN -> NJN
.
This replaces all cols but I want a subset of columns:
df = df.replace(name_dict)
Daisy-chaining 'replace' works:
df = df.replace({'col1': name_dict}).replace({'col2': name_dict})
This also works:
df = df.replace({'col1': name_dict, 'col2': name_dict})
Can I shorten this up a bit, something like:
df = df.replace({['col1', 'col2']: name_dict})
However, this results in TypeError: unhashable type: 'list'
.
Desired output (col1
and col2
values updated but not col3
):
id col1 col2 col3
0 a PHO X PHX
1 b NJN PHO BKN
2 c X NJN PHX
3 d PHO NJN BKN
4 e X X PHX
Solution
Using dict.fromkeys
:
df.replace(dict.fromkeys(['col1', 'col2'], name_dict))
Or, if you want to modify in place, with a list of columns:
cols = ['col1', 'col2']
df[cols] = df[cols].replace(name_dict)
Output:
id col1 col2 col3
0 a PHO X PHX
1 b NJN PHO BKN
2 c X NJN PHX
3 d PHO NJN BKN
4 e X X PHX
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.