Reputation: 611
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]
?
Upvotes: 22
Views: 94331
Reputation: 2834
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
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
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.
Upvotes: 5
Reputation: 85432
You can use iloc
:
df.iloc[:, 0]
Example:
>>> df
a b c
0 1 4 7
1 2 5 8
2 3 6 9
>>> df['a']
0 1
1 2
2 3
Name: a, dtype: int64
>>> df.iloc[:, 0]
0 1
1 2
2 3
Name: a, dtype: int64
Upvotes: 23