0

Let's say I have dataframe in python as defined below. The columns are X, Y,Z.

import pandas as pd
_dict={}
_dict['X'] =[90,75,95,77]
_dict['Y'] =[87,85,92,65]
_dict['Z'] =[93,90,91,78]
df=pd.DataFrame(_dict)
X Y Z
90 87 93
75 90 85
95 92 91
77 65 78

I have to create 2 new columns, max and max_column.
max: Will contain row-wise maximum value among X,Y,Z.
max_column: Will contain the name of the column which from which maximum value was obtained.

X Y Z max max_column
90 87 93 93 Z
75 90 85 90 Y
95 92 91 95 X
77 65 78 78 Z

Can someone help me with this?

I tried this to get maxcolumn, need help with creating max_column.

def maximum(row):
    if (row['X']>row['Y']) and (row['X']>row['Z']):
        return row['X']
    elif (row['Y']>row['X']) and (row['Y']>row['Z']):
        return row['Y']
    else:
        return row['Z']
df['max']=df.apply(lambda x: maximum(x), axis=1)

0 Answers0