My data is formatted as shown in the picture what i want is for each location the highest mortality year in python pandas and the data shown in the image is a python pandas dataframe...
What i did want tried to get the max value by doing this df.max(axis=1) but that just returned the value not the key with it....
CodePudding user response:
Just use the following:
df.loc[:, df.columns[1:]].idxmax(axis=1)
Example:
input df:
output:
code:
df = pd.DataFrame(data = [['jim', 1, 2, 3],['john', 5, 6, 12], ['jack', 100, 10, 15]],
columns = ['user', 'm1', 'm2', 'm3'])
df.loc[:,df.columns[1:]].idxmax(axis=1)