我希望更新满足特定条件的熊猫系列中的值,并从另一列中获取相应的值。
具体来说,我想查看subcluster
列,如果值等于 1,我希望记录更新为cluster
列中的相应值。
例如:
簇 | 子集群 |
---|---|
3 | 1 |
3 | 2 |
3 | 1 |
3 | 4 |
4 | 1 |
4 | 2 |
应该导致这个
簇 | 子集群 |
---|---|
3 | 3 |
3 | 2 |
3 | 3 |
3 | 4 |
4 | 4 |
4 | 2 |
我一直在尝试使用 apply 和 lambda 函数,但似乎无法正常工作。任何建议将不胜感激。谢谢!
在你的情况下尝试 mask
df.Subcluster.mask(lambda x : x==1, df.Cluster,inplace=True)
df
Out[12]:
Cluster Subcluster
0 3 3
1 3 2
2 3 3
3 3 4
4 4 4
5 4 2
或者
df.loc[df.Subcluster==1,'Subcluster'] = df['Cluster']
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句