7

My data is like this:

[First row is headers]

Name,Email,Age
Sachith,ko@gmail.com,23
Sim,sm@gmail.com,234
Yoshi,yosi@hotmail.com,2345
sarla,sarla@gmail.com,234

I would like to access elements such that rows are specified as integers and columns by labels. i.e for Sim I would like to access it as [1,'Name'] and so-on

My question is should I use loc or ix?

Looking at the documentation, I am confused as to what is a pandas index? Is it used to access rows or columns or both? When I try to print the indices of this data, I get a (4,) dtype=int64 array of [0,1,2,3]. So , are columns not part of the index?

Bonifacio2
  • 3,405
  • 6
  • 34
  • 54
sir_osthara
  • 154
  • 2
  • 9

1 Answers1

12

Use loc or iloc, because ix is deprecated.

print (df)
      Name             Email   Age
0  Sachith      ko@gmail.com    23
1      Sim      sm@gmail.com   234
2    Yoshi  yosi@hotmail.com  2345
3    sarla   sarla@gmail.com   234

#select by label 1 and label Name    
a = df.loc[1, 'Name']
print (a)
Sim

But if need select index by position (need iloc) and columns by labels (need loc) together:

df = df.set_index('Email')
print (df)
                     Name   Age
Email                          
ko@gmail.com      Sachith    23
sm@gmail.com          Sim   234
yosi@hotmail.com    Yoshi  2345
sarla@gmail.com     sarla   234

get label of second index by df.index[1]:

a = df.loc[df.index[1], 'Name']
print (a)
Sim

Or get position of label by get_loc:

a = df.iloc[1, df.columns.get_loc('Name')]
print (a)
Sim
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • But , it is mentioned that loc only works with label based indexer. However, aren't the indices here integers? Please correct me if I am wrong. – sir_osthara Jul 26 '17 at 14:46
  • Thank you for the detailed response. I am still unclear on one thing. What is the difference between df.loc[1, 'Name'] and df.loc[df.index[1], 'Name']? I don't understand how df.loc[1, 'Name'] as the 1 is an integer and loc only takes label based indexing. – sir_osthara Jul 26 '17 at 15:14
  • 1
    Oh, I think I get it, correct me if I am wrong. if the index is sequential and starts from 0, then label of a index is equal to its position. So, df.loc[1,'Name'] is actually looking for the index with label 1 and it happens to be at position 1 – sir_osthara Jul 26 '17 at 15:16