[英]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]})
例如,我的df如下所示:
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. 另外,执行非常低效的
groupby
和apply
组合工作,但显然不是最佳选择。
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
: 或者您可以使用
update
:
df.update(df.loc[df.B.isin([101, 103])].replace(d))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.