likeyears
likeyears

Reputation: 51

Pandas: How to get the location of value without column name and index

There is a dataframe below:

Unnamed:0|Unnamed:1|Unnamed:2
Apple    |         |50
Orange   |         |60
Banana   |         |70

How can I get the location of value '60' by method df[index, index] without column name?
And the value of '60' is a dynamic result, I only can make sure its location is fixed( at the same row of 'Orange', and next to it with two columns)

Upvotes: 1

Views: 2651

Answers (2)

jezrael
jezrael

Reputation: 863226

I think you need for selecting second row and third column methods DataFrame.iloc DataFrame.insert or DataFrame.iat, there is used [1,2] because pandas count from 0 - so first Apple should be selected by [0,0]:

df.iloc[1,2]

Or:

df.iat[1,2]

For Orange use second row, first column:

df.iloc[1,0]
df.iat[1,0]

EDIT:

If need third column same like Orange position:

df.iloc[(df['fruit'] == 'Orange').to_numpy(), 2]

Upvotes: 2

Icarus
Icarus

Reputation: 455

maybe something like

import pandas as pd
data = {'fruit': ['Apple', 'Orange', 'Banana'], 
        'price': ['50', '60', '70']}  
df = pd.DataFrame.from_dict(data) 
df.loc[df['fruit'] == 'Orange']

Upvotes: 0

Related Questions