Let's say that I want to find the person that ate the most tacos from a taco eating competition.
DataFrame
df = pd.DataFrame({'tacos':[5,10,7]},index=['John','Carlos','Peter'])
If I have identified the max tacos a person has eaten, then how I could get the name of that person as a string?
maxtacos = df['Tacos'].max()
df.get(df[Name], df['Tacos', maxtacos])
I would like to get "Carlos" as a result.
Thank you
df.loc[df['Tacos'].eq(df['Tacos'].max(), 'Name'].tolist()
You can have more than one person...df = pd.DataFrame({'Tacos': [5, 5, 1], 'Name': ['John', 'Harry', 'Bill']})
. Note thatidxmax
only gives you the first name that matches the max value, the method above gives you the list of all names, i.e.df.loc[df['Tacos'].eq(df['Tacos'].max()), 'Name'].tolist()
results in['John', 'Harry']
.