Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new column with existing column names

Tags:

python

pandas

max

I'm dealing with a dataframe which looks like:

    FID           geometry                                       Code   w1  w2
0   12776   POLYGON ((-1.350000000000025 53.61540813717482...   12776   0   1
1   13892   POLYGON ((6.749999999999988 52.11964001623148,...   13892   1   0
2   14942   POLYGON ((-3.058896639907732e-14 51.3958198431...   14942   1   1
3   18964   POLYGON ((8.549999999999974 45.26941059233587,...   18964   0   1
4   19863   POLYGON ((-0.4500000000000305 44.6337746953077...   19863   0   1

My objective is to add a column, labeled as 'Max', where I'm going to write which w (w1, w2) has got more frequency.

So far I've only managed add a column in which appears the maximum frequency, instead of the name of the column where it appears.

The desired output would be something like this:

    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

Furthermore, I'd like to fill with zeros whenever the frequencies are the same, if its possible, at the same time.

Any help would be appreciated! :-)

like image 217
Gonzalo Donoso Avatar asked Jan 05 '23 17:01

Gonzalo Donoso


1 Answers

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
like image 153
piRSquared Avatar answered Jan 16 '23 22:01

piRSquared