Prince Kumar
Prince Kumar

Reputation: 23

How do I find the index of a particular element from a particular column

I want to get the numerical position of a particular element of a column

Upvotes: 0

Views: 58

Answers (3)

jezrael
jezrael

Reputation: 862641

Use numpy.where with mask only for integer position:

df = pd.DataFrame({'a':[1,2,3]}, index=list('ABC'))

i = np.where(df['a'].eq(2))[0]
print (i)
[1]

Upvotes: 1

Abhishek Kulkarni
Abhishek Kulkarni

Reputation: 1767

Try this below :

For eg:

import pandas as pd

df = pd.DataFrame({'date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/2011'], 
                   'product':['p1', 'p2', 'p3', 'p4'], 
                   'latest_price':[1200, 1500, 1600, 352], 
                   'new_price':[1250, 1450, 1550, 400], 
                   'Discount':[10, 10, 10, 10]}, index=['0', '1', '2', '3']) 


print(df) 


index = df[df['new_price']<500].index        # --------> Here you can give any condition to get your index.

print(index)

Upvotes: 0

Raccoon_17
Raccoon_17

Reputation: 163

I think you can find your answer in here: Python Pandas: Get index of rows which column matches certain value

Also, if you want to get your answer using Openpyxl, you can use the following:

for row in ws.iter_rows():
    for cell in row:
        if cell.value == "test123":
            print(cell.row) //Prints row '1'
            print(cell.column) //Prints column '2'
            print(cell.coordinate) //Prints coordinate 'A5'


Or you can store the values from cell.column/cell.row in a variable and use it later.

Hope that helps :)

Upvotes: 0

Related Questions