简体   繁体   中英

Using .loc and .replace() Together

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?

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM