![](/img/trans.png)
[英]Replace values of a Pandas dataframe's Column based on values of another column
[英]Pandas replace values of a column with comparison to another Dataframe
如果我正确理解了这个问题,您希望将表中的CustomerID
中的值替换为unicite
列中的值(如果它们存在于表Newvalue
的CustomerID
列中Fidclients
。
我相信你必须合并表格才能实现这一点。 例如,
unicite = pd.DataFrame({'CustomerID': ['a', 'b', 'c']})
print(unicite)
CustomerID
0 a
1 b
2 c
Fidclients = pd.DataFrame({'CustomerID': ['c', 'f', 'g'], 'Newvalue': ['x', 'y', 'z']})
print(Fidclients)
CustomerID Newvalue
0 c x
1 f y
2 g z
merged = unicite.merge(Fidclients, on='CustomerID', how='left')
merged.loc[merged.Newvalue.notnull(), 'CustomerID'] = merged.Newvalue
merged.drop('Newvalue', axis=1)
CustomerID
0 a
1 b
2 x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.