Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take the maximum in absolute value from different columns and filter out NaN Python

This was my try. For example

df = pd.DataFrame({'a':[5,0,1,np.nan], 'b':[np.nan,1,4,3], 'c':[-3,-2,0,0]})
df.dropna(axis=1).max(axis=1,key=abs)

Filters out well the NaN values but it gets 0 or negative values instead of the highes in absolute value

The result should be one column with

5
-2
4
3
like image 590
gis20 Avatar asked Dec 07 '15 10:12

gis20


1 Answers

The most straightforward and efficient way is to convert to absolute values, and then find the max. Pandas supports this with straightforward syntax (abs and max) and does not require expensive apply operations:

df.abs().max()

max() accepts an axis argument, which can be used to specify whether to calculate the max on rows or columns.

like image 166
Brendan Avatar answered Sep 18 '22 20:09

Brendan