Bit Magazin
Bit Magazin

Reputation: 11

Taking index value of a dataframe pandas

I'm trying to get the index value of an element in the dataframe in pandas. I'm taking two values (date and price) from a database and putting in dataframe using pandas. I found how to find min and max value of price but for those values I want to know print out which date it is. I tried to use numpy where function but I couldn't figure it out. I'm noob sorry. Here is my code. Thank you..

import sqlite3
import pandas as pd
import numpy as np




conn = sqlite3.connect('price_daily.sqlite')
cur = conn.cursor()

tables_prices='''SELECT date, tryprice FROM Btc'''

df=pd.read_sql_query(tables_prices, conn)    #print(df.head())

x=df['tryprice'].max()

#I want to return date as string for this element in dataframe...

Upvotes: 1

Views: 118

Answers (1)

piRSquared
piRSquared

Reputation: 294516

Use idxmax and idxmin

To get the entire row associated with the minimum tryprice

df.loc[df['tryprice'].idxmin()]

To get the Date column of the row associated with the minimum tryprice

df.at[df['tryprice'].idxmin(), 'date']

Upvotes: 1

Related Questions