繁体   English   中英

根据另一列中的条件在 Pandas 数据框中设置值

[英]Setting Values in Pandas Dataframe Based on Condition in Another Column

我希望更新满足特定条件的熊猫系列中的值,并从另一列中获取相应的值。

具体来说,我想查看subcluster cluster列,如果值等于 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 函数,但似乎无法正常工作。 任何建议将不胜感激。 谢谢!

您可以使用np.where

import numpy as np

df['Subcluster'] = np.where(df['Subcluster'].eq(1), df['Cluster'], df['Subcluster'])

输出:

    Cluster  Subcluster
0         3           3
1         3           2
2         3           3
3         3           4
4         4           4
5         4           2

在你的情况下尝试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']

在这里,您真正需要的只是将 .loc 与掩码一起使用(您实际上不需要创建掩码,您可以内联应用掩码)

df = pd.DataFrame({'cluster':np.random.randint(0,10,10)
                    ,'subcluster':np.random.randint(0,3,10)}
                 )
df.to_clipboard(sep=',')

df此时

,cluster,subcluster
0,8,0
1,5,2
2,6,2
3,6,1
4,8,0
5,1,1
6,0,0
7,6,0
8,1,0
9,3,1

创建并应用蒙版(您可以在一行中完成所有操作)

mask = df.subcluster == 1
df.loc[mask,'subcluster'] = df.loc[mask,'cluster']
df.to_clipboard(sep=',')

最终输出:

,cluster,subcluster
0,8,0
1,5,2
2,6,2
3,6,6
4,8,0
5,1,1
6,0,0
7,6,0
8,1,0
9,3,3

这是您无法编写的 lambda。 在 Lamba 中, x对应于索引,因此您可以使用它来引用列中的特定行。

df['Subcluster'] = df.apply(lambda x: x['Cluster'] if x['Subcluster'] == 1 else x['Subcluster'], axis = 1)

和输出:

    Cluster Subcluster
0   3       3
1   3       2
2   3       3
3   3       4
4   4       4
5   4       2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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