Skip to content Skip to sidebar Skip to footer

Add New Column With Existing Column Names

I'm dealing with a dataframe which looks like: FID geometry Code w1 w2 0 12776 POLYGON ((-1.350000000000025 53.6154081371

Solution 1:

Use np.where to choose 0 when they are equal idxmax(1) when they are not.

df['max'] = np.where(df.w1 == df.w2, 0, df[['w1', 'w2']].idxmax(1))
df

    FID     geometry     Code   w1  w2   Max
0   12776   ...         12776   0   1    w2
1   13892   ...         13892   1   0    w1
2   14942   ...         14942   1   1    0
3   18964   ...         18964   0   1    w2
4   19863   ...         19863   0   1    w2

Solution 2:

Something like this should work:

(df['w1'] == df['w2']).map({True: 0}).fillna(df[['w1', 'w2']].idxmax(axis=1))
Out[26]: 
0    w2
1    w1
2     0
3    w2
4    w2
dtype: object

How it works:

The main part is with idxmax:

df[['w1', 'w2']].idxmax(axis=1)
Out[27]: 
0w21w12w13w24w2dtype: object

This first selects the relevant columns, and returns the index of the maximum (axis=1 for columns). However, it returns the first index in case of ties.

(df['w1'] == df['w2']).map({True: 0}) fills a series with 0 when w1==w2. Remaining values are NaN. So those are filled with idxmax values.

Note: np.where is definitely the more logical (and probably faster) choice here. I just like to experiment with other alternatives.

Post a Comment for "Add New Column With Existing Column Names"