Home > Software engineering >  For each country/location, find which year had the highest mortality in Python
For each country/location, find which year had the highest mortality in Python

Time:02-28

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

enter image description here

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:

enter image description here

output:

enter image description here

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)
  • Related