23

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]?

2 Answers 2

24

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
Sign up to request clarification or add additional context in comments.

2 Comments

How can apply group by column b with out using column name
Use df.groupby(df.columns[1]).
5

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

Access rows and columns by integer position(s)

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

Access rows and columns by label(s)

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.