Home > Software engineering >  python pandas | conditional min()
python pandas | conditional min()

Time:11-20

volume price datetime
400 9 2021-09-29 04:00:00-04:00
900 22 2021-09-29 06:00:00-04:00
1000 31 2021-09-29 08:00:00-04:00
400 21 2021-09-29 10:00:00-04:00
1200 31 2021-09-29 12:00:00-04:00

I'd like to find the datetime where the highest price ($31) is.

If the highest price appears twice, return the earlier datetime.

Expected output should be 2021-09-29 08:00:00-04:00.

CodePudding user response:

I think you're looking for pandas.Series.idxmax():

row = df.loc[df['price'].idxmax()]

Output:

>>> row
volume                           1000
price                              31
datetime    2021-09-29 08:00:00-07:00
Name: 2, dtype: object
  • Related