比较所有行的多个特定列

时间:2018-10-29 11:37:59

标签: python pandas numpy dataframe

我想比较所有行的特定列,如果它们是唯一的,则将值提取到新列,否则为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

我很高兴听到一些建议。

2 个答案:

答案 0 :(得分: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按列名列表过滤的所有列与列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

然后按all检查每行是否所有True

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

最后一次使用numpy.whereTrue分配第一列值,为0分配False

答案 1 :(得分: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
相关问题