比较所有行的多个特定列

问题描述 投票:0回答:2

我想比较所有行的特定列,如果它们是唯一的,则将值提取到新列,否则为0。

如果示例数据帧如下:

A      B      C  D  E  F
13348  judte  1  1  1  1
54871  kfzef  1  1  0  1
89983  hdter  4  4  4  4
7543   bgfd   3  4  4  4

结果应如下:

A      B      C  D  E  F Result
13348  judte  1  1  1  1  1
54871  kfzef  1  1  0  1  0
89983  hdter  4  4  4  4  4
7543   bgfd   3  4  4  4  0

我很高兴听到一些建议。

python pandas numpy dataframe
2个回答
3
投票

使用:

cols = ['C','D','E','F']

df['Result'] = np.where(df[cols].eq(df[cols[0]], axis=0).all(axis=1), df[cols[0]], 0)
print (df)
       A      B  C  D  E  F  Result
0  13348  judte  1  1  1  1       1
1  54871  kfzef  1  1  0  1       0
2  89983  hdter  4  4  4  4       4
3   7543   bgfd  3  4  4  4       0

详情:

首先将eq列名列表过滤的所有列与cols df[cols[0]]的第一列进行比较:

print (df[cols].eq(df[cols[0]], axis=0))
      C      D      E      F
0  True   True   True   True
1  True   True  False   True
2  True   True   True   True
3  True  False  False  False

然后通过True检查每行是否所有alls:

print (df[cols].eq(df[cols[0]], axis=0).all(axis=1))
0     True
1    False
2     True
3    False
dtype: bool

最后使用numpy.whereTrue分配0s和False的第一列值。


2
投票

我认为你需要applynunique

df['Result'] = df[['C','D','E','F']].apply(lambda x: x[0] if x.nunique()==1 else 0,1)

或者使用np.where

df['Result'] = np.where(df[['C','D','E','F']].nunique(1)==1,df['C'],0)

print(df)
       A      B  C  D  E  F  Result
0  13348  judte  1  1  1  1       1
1  54871  kfzef  1  1  0  1       0
2  89983  hdter  4  4  4  4       4
3   7543   bgfd  3  4  4  4       0
© www.soinside.com 2019 - 2024. All rights reserved.