0

Been trying to search for this but somehow can't seem to find the right answer.

Given the following simple dataframe:

   country   continent    population
0    UK       Europe        111111
1   Spain     Europe        222222
2   Malaysia   Asia         333333
3    USA      America       444444

How can I retrieve the country value if I have a condition WHERE an index value is given? For example, If I am given an index value of 2, I should return Malaysia.

Edit: Forget to mention that the input index value comes from a variable (think of it as a user select a particular row and the selected row provide an index value variable).

Thank you.

1
  • Use loc - df.loc[index,'country'] Commented May 21, 2020 at 15:36

2 Answers 2

1
df.iloc[2]['country']

iloc is used for selection by position, see pandas.DataFrame.iloc documentation for further options.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi there, thanks for the reply! Sorry I forget to mention a condition. Please take a look at my edit.
0
index = 2    
print(df.iloc[index]['country'])

Malaysia

3 Comments

Hi there, thanks for the reply! Sorry I forget to mention a condition. Please take a look at my edit.
Check the edited answer, is that what you're looking for?
Doh, silly me. It is - can't believe I spend hours searching for it. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.