Skip to content
Advertisement

Groupby names replace values with there max value in all columns pandas

I have this DataFrame

JavaScript

which looks like this

JavaScript

I want this

JavaScript

replaced all values with the maximum value. we choose the maximum value from both val1 and val2

if i do this i will get the maximum from only val1

JavaScript

Advertisement

Answer

Try using pd.wide_to_long to melt that dataframe into a long form, then use groupby with transform to find the max value. Map that max value to ‘name’ and reshape back to four column (wide) dataframe:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement