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
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.
Comments