Amit
Amit

Reputation: 793

getting another column is one column of data has maximum value in python?

I have a data frame

date    object   price
190403    a        1
190405    a       23
190506    b       -4
190507    d       56

I want to get a date of a column having a maximum Price i.e 190507

expected output 190507

Upvotes: 0

Views: 31

Answers (2)

CHRD
CHRD

Reputation: 1957

You can subset the df so that you only have the row where the price column is at its maximum, and then choose the date column:

df[df.price==df.price.max()].date

Upvotes: 0

jezrael
jezrael

Reputation: 862471

For scalar output, always one max date value use Series.idxmax with convert date to index by DataFrame.set_index:

df.set_index('date')['price'].idxmax()

If want all max values in Series use boolean indexing and compare all values by max, DataFrame.loc is fir also filtering date column:

df.loc[df['price'].eq(df['price'].max()), 'date']

Upvotes: 1

Related Questions