5
data = [['tom', 10], ['nick', 15], ['juli', 14]] 
df = pd.DataFrame(data, columns = ['Name', 'Age'], index = [7,3,9])
display(df)
df.iat[0,0]

enter image description here

I'd like to return the Age in first row (basically something like df.iat[0,'Age']. Expected result = 10

Thanks for your help!

1

2 Answers 2

7

df['Age'].iloc[0] works too, similar to what Chris had answered.

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

2 Comments

That is exactly what I was looking for, thanks. iloc vs loc is a bit confusing...I thought that the "i" in iloc refers to index not row number, but it is the other way around! :-)
@ChadeeFouad My thinking was the same! Overall , I wish the Pandas programmatic experience was bit more intuitive.
4

Use iloc and Index.get_loc:

df.iloc[0, df.columns.get_loc("Age")]

Output:

10

5 Comments

Thanks...but I was looking for a straightforward command...I'm kind of surprised that it doesn't exist?
A method that may take both integer index (iloc) and/or name/index will be extremely vulnerable: row index with 0 is not necessarily row with integer index 0, vice versa, and so is for columns, and pandas will never know the context of user input.
well I have lots of situations where a dataframe is sorted and I just need to take values from the first row whatever it is. I guess your solution is the only way. I'll accept your answer. Thanks
@ShadyMBA If its always the first row, how about df.head(1)["Age"]?
That's a smart solution Chris!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.