Home > Enterprise >  python: How to keep only max value of row and convert other value to NA?
python: How to keep only max value of row and convert other value to NA?

Time:03-07

How to keep only max value of row and convert other value to NA?

d = {'col1': [1, 2, 3], 'col2': [3, 4, 5],'col3': [6, 7, 8]}
df = pd.DataFrame(data=d)
df

expected output 
d = {'col1': np.nan, 'col2': np.nan,'col3':[6, 7, 8]}
df = pd.DataFrame(data=d)
df

CodePudding user response:

Try with where

out = df.where(df.eq(df.max(1),axis=0))
Out[599]: 
   col1  col2  col3
0   NaN   NaN     6
1   NaN   NaN     7
2   NaN   NaN     8
  • Related