0

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

I AM A Hacker
  • 113
  • 1
  • 1
  • 10
  • 1
    Please [do not post images of code](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). With that out of the way, you probably need [`idxmax`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.idxmax.html). – fsimonjetz Feb 27 '22 at 12:33

1 Answers1

1

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)
keramat
  • 4,328
  • 6
  • 25
  • 38