Reputation: 1131
How can I update a dataframe's columns values based on dictionary?
For example, I have df that looks like:
df = pd.DataFrame({'B' : [100,101,102,103],'E' : pd.Categorical(["test","train","test","train"]), 'F' : [128,300,1205,2000]})
Out[28]:
B E F
0 100 test 128
1 101 train 300
2 102 test 1205
3 103 train 2000
dict = {300:301, 2000:2001}
df.loc[df['B'].isin([101,103])].replace(dict)
Out[31]:
B E F
1 101 train 301
3 103 train 2001
This gives the proper results but doing this inplace gives a Copy Warning and I need to update the original dataframe with this logic.
Also, doing a very inefficient groupby
& apply
combo works but clearly not optimal.
How can I accomplish this?
Upvotes: 1
Views: 4913
Reputation: 214967
You can assign the result back to the same positions of the data frame:
d = {300:301, 2000:2001}
mask = df.B.isin([101, 103])
df.loc[mask] = df.loc[mask].replace(d)
df
# B E F
#0 100 test 128
#1 101 train 301
#2 102 test 1205
#3 103 train 2001
Or you can use update
:
df.update(df.loc[df.B.isin([101, 103])].replace(d))
Upvotes: 3