4
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!

Community
  • 1
  • 1
Chadee Fouad
  • 2,630
  • 2
  • 23
  • 29

2 Answers2

6

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

Randy Maldonado
  • 352
  • 1
  • 7
  • 1
    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! :-) – Chadee Fouad Jan 10 '20 at 05:28
  • 1
    @ChadeeFouad My thinking was the same! Overall , I wish the Pandas programmatic experience was bit more intuitive. – Sau001 Feb 22 '22 at 21:23
4

Use iloc and Index.get_loc:

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

Output:

10
Chris
  • 29,127
  • 3
  • 28
  • 51
  • Thanks...but I was looking for a straightforward command...I'm kind of surprised that it doesn't exist? – Chadee Fouad Jan 10 '20 at 04:56
  • 1
    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. – Chris Jan 10 '20 at 05:00
  • 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 – Chadee Fouad Jan 10 '20 at 05:09
  • 1
    @ShadyMBA If its always the first row, how about `df.head(1)["Age"]`? – Chris Jan 10 '20 at 05:10
  • That's a smart solution Chris! – Chadee Fouad Mar 18 '20 at 22:44