Python Dataframe rowwise min and max with NaN values

Mainland

My dataframe has nans. But I am trying to find row wise min and max. How do I find it.

df = pd.DataFrame({"A":[12,NaN,13],"B":[45,12,65],"C":[63,78,NaN]})

df= 
    A    B     C
0   12   45    63
1   NaN  12   78
2   13   65    NaN

I am tring to find min and max in each row and difference between them in column A B and C. My code:

poacols = ['A','B','C']
df['dif'] = np.nanmax(df[poacols])-np.nanmin(df[poacols])

Present output:

df['dif'] = 
0    66
1    66
2    66

Expected output:

df['dif'] = 
0    51
1    66
2    52
BENY

We should add the axis=1, check the min and max for each row

np.nanmax(df[poacols],1)-np.nanmin(df[poacols],1)
Out[81]: array([51., 66., 52.])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why do NaN values make min and max sensitive to order?

Max and Min values within pandas (sub)Dataframe

max and min with NaN in Haskell

Min and Max values for EditorFor

Max / Min of date column in Pandas, columns include nan values

Rowwise min() and max() fails for column with NaNs

Find min/max values in a dataframe containing a mixture of integers and strings

Reordering rowwise values in a dataframe

selecting rows with min and max values in pandas dataframe

Return rows with max/min values at bottom of dataframe (python/pandas)

R: How to to replace(switch) the max and min values in a row in a dataframe when max <= min?

Max and min values in pandas

python get max and min values across mutiple columns while grouping a dataframe

Filter dataframe for values that are below or exceed min/max value that is specified in a column

Finding Min and Max Values in List Pattern in Python

How to round and apply min and max to all values in Pandas Dataframe

math min and max returns NaN

How to get the max/min value in Pandas DataFrame when nan value in it

Python - Select min values in dataframe

Rowwise max/min in pandas

How to find the difference of max & min values in one group in a variable in a dataframe

Min and Max Values are incorrect

get max and min values based on conditions in pandas dataframe

Returning the max and min values and indexes with Numpy Python

Python Dataframe Compute Min and Max for list of elements

Get min and max values of categorical variable in a dataframe

Python Pandas Flag for min max Values

How to convert max values of a multiple groupby dataframe to nan?

Find Top5 min and max values per group in dataframe (determine min and max differentially expressed genes)