How can I call a column in my code using its index in the dataframe instead of its name.
For example, I have dataframe dfwith columns a, b,c
df
a
b
c
Instead of calling df['a'], can I call it using a column index like this df[1]?
df['a']
df[1]
You can use iloc:
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