Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call column in dataframe by column index instead of column name - pandas

How can I call column in my code using its index in dataframe instead of its name.

For example I have dataframe df with columns a, b, c

Instead of calling df['a'], can I call it using its column index like df[1]?

like image 620
balakishore nadella Avatar asked Mar 30 '16 17:03

balakishore nadella


1 Answers

The indexing and selecting data documentation mentions that the indexing operator [] is provided more for convenience. The iloc and loc methods provide more explicit indexing operations on the dataframe.

Note: Index has its own connotation in pandas. So when referring to the numeric index (like an array index), it is better to use interger position (or just position).

>>> df
   a  b
0  1  4
1  2  5
2  3  6

>>> df['a']
0    1
1    2
2    3
Name: a, dtype: int64

Access rows and columns by integer position(s)

df.iloc[row_start_position:row_end_position, col_start_position:col_end_position]

>>> df.iloc[0:3, 0:1]
   a
0  1
1  2
2  3

>>> df.iloc[:, 0]  # use of implicit start and end
0    1
1    2
2    3
Name: a, dtype: int64

Access rows and columns by label(s)

df.loc[row_start_label:row_end_label, col_start_label:col_end_label]

Note: In this example, it just so happens that the row label(s) and the row position(s) are are the same, which are integers 0, 1, 2.

>>>  df.loc[0:2, 'a':'a']
   a
0  1
1  2
2  3

>>> df.loc[:, 'a'] # use of implicit start and end
0    1
1    2
2    3
Name: a, dtype: int64

See how to Query / Select / Slice Data for more details.

like image 174
ap-osd Avatar answered Sep 20 '22 12:09

ap-osd