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]
?
Advertisement
Answer
You can use iloc
:
JavaScript
x
2
1
df.iloc[:, 0]
2
Example:
JavaScript
1
18
18
1
>>> df
2
a b c
3
0 1 4 7
4
1 2 5 8
5
2 3 6 9
6
7
>>> df['a']
8
0 1
9
1 2
10
2 3
11
Name: a, dtype: int64
12
13
>>> df.iloc[:, 0]
14
0 1
15
1 2
16
2 3
17
Name: a, dtype: int64
18