Pandas How To Uncategorized How to find index of value in Pandas

How to find index of value in Pandas

To find the index of a value in a pandas DataFrame, you can use the idxmax or idxmin method, or the query method.

For example, if you have a DataFrame named df and you want to find the index of the row where the value in the col1 column is maximum, you can use the following code:

idx = df['col1'].idxmax()

To find the index of the row where the value in the col1 column is minimum, you can use the following code:

idx = df['col1'].idxmin()

If you want to find the index of a row that meets a certain condition, you can use the query method. For example, to find the index of the row where the value in the col1 column is greater than 30, you can use the following code:

idx = df.query('col1 > 30').index[0]

The query method returns a new DataFrame that meets the condition, and you can extract the index of the first row using the index attribute and the [0] index.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post