ali
ali

Reputation: 151

compare multiple specific columns of all rows

I want to compare the particular columns of all the rows, if they are unique extract the value to the new column otherwise 0.

If the example dateframe as follows:

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

The result should be as follows:

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

I am pleased to hear some suggestions.

Upvotes: 0

Views: 65

Answers (2)

Space Impact
Space Impact

Reputation: 13255

I think you need apply with nunique as:

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

Or using 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

Upvotes: 2

jezrael
jezrael

Reputation: 862761

Use:

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

Detail:

First compare all column filtered by list of columns names by eq with first column of 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

Then check if all Trues per row by all:

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

And last use numpy.where for assign first column values for Trues and 0 for False.

Upvotes: 3

Related Questions