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
:
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