Reputation: 2978
I have the following DataFrame df
:
col1 col2
3 A
3 B
5 A
1 C
6 B
How can I replace 3 by 0 and 5 by 1, while the rest of numbers should be replaced by 2?
Expected result:
col3 col2
0 A
0 B
1 A
2 C
2 B
This is what I wrote so far:
vals = {3:0, 5:1}
df["col3"] = df["col1"].map(vals)
Upvotes: 0
Views: 32
Reputation: 8816
You already close to what you need, just use df.assign
to create a new column and replace nan
values with DataFrame.replace
and convert later to int.
>>> df.assign(col3=df["col1"].map(vals).replace(np.nan, 2).astype(int))
col1 col2 col3
0 3 A 0
1 3 B 0
2 5 A 1
3 1 C 2
4 6 B 2
On the top of it as you are looking to have only col2
and col3
hence you can drop col1
as follows ..
>>> df.assign(col3=df["col1"].map(vals).replace(np.nan, 2).astype(int)).drop(columns={'col1'})
# df = df.assign(col3=df["col1"].map(vals).replace(np.nan, 2).astype(int)).drop(columns={'col1'})
col2 col3
0 A 0
1 B 0
2 A 1
3 C 2
4 B 2
Upvotes: 1
Reputation: 862641
Because map
return missing values for non matched values, add Series.fillna
:
vals = {3:0, 5:1}
df["col3"] = df["col1"].map(vals).fillna(2).astype(int)
print (df)
col1 col2 col3
0 3 A 0
1 3 B 0
2 5 A 1
3 1 C 2
4 6 B 2
Upvotes: 2