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! :-)
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
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With