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

时间:2021-07-07 14:47:38

标签: python pandas dataframe function

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

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

4 个答案:

答案 0 :(得分:2)

您可以使用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

答案 1 :(得分:1)

就您而言,请尝试 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']

答案 2 :(得分:0)

这里你真正需要的只是将 .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

答案 3 :(得分:0)

这是您无法编写的 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
相关问题