Cecilia Gonzalez
Cecilia Gonzalez

Reputation: 1

Python DataFrame with two instances under same index

I'm relatively new to Python. I've been working in a project, and have a DataFrame that gives me two different instances for the same index. I exlpain: When I ask for an instance this way:

df[df.name == 'Marcia']

the result shows Int64Index([92], dtype='int64')

But if I ask for an instance by index = 92 this way:

df.iloc[92]

the result is a different instance, whose 'name' is not 'Marcia'. There is only one 'Marcia' in my dataset. How can this happen?

Upvotes: 0

Views: 127

Answers (1)

Rik Kraan
Rik Kraan

Reputation: 586

df.iloc[92] takes the 92th row in your dataframe. As your dataframe is shuffled, or some rows have been deleted during data wrangling steps, the row with index-name 92 may not be the 92th row anymore.

Try using df.loc[92] instead, as it returns the row with index-name 92.

See How are iloc and loc different? for more information.

Upvotes: 1

Related Questions