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]
?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With