blueseal
blueseal

Reputation: 355

Create pandas column with a condition based on a range of columns

I want to add a new column that if a range of 5 columns all have the same value, it will have that value. Otherwise, it will have "Error".

From this:

|1 1 1 1 1|
|2 2 2 2 2|
|0 1 2 2 1|
|z z z z z|

To this:

|1 1 1 1 1| 1
|2 2 2 2 2| 2
|0 1 2 2 1| Error
|z z z z z| z

Is there a way to do this without iterating over each row?

Upvotes: 1

Views: 520

Answers (1)

jezrael
jezrael

Reputation: 862406

Use #numpy.where with test same values by DataFrame.nunique:

df['new'] = np.where(df.nunique(axis=1).eq(1), df.iloc[:, 0], 'error')

Or for better performance test first column if same values like all columns:

df['new'] = np.where(df.eq(df.iloc[:, 0], axis=0).all(1), df.iloc[:, 0], 'error')
print (df)
   0  1  2  3  4    new
0  1  1  1  1  1      1
1  2  2  2  2  2      2
2  0  1  2  2  1  error
3  z  z  z  z  z      z

Upvotes: 2

Related Questions