Pandas How To Uncategorized How to select columns by number in Pandas

How to select columns by number in Pandas

In Pandas, you can select columns by number using the .iloc indexer. The .iloc indexer is used to select data by position, and allows you to select rows and columns using integer positions.

To select columns by number, you can pass a slice or a list of integers representing the column positions to the .iloc indexer. Here are some examples:

import pandas as pd

# create a sample dataframe
data = {'name': ['John', 'Jane', 'Bob'],
'age': [30, 25, 40],
'city': ['New York', 'Paris', 'London']}
df = pd.DataFrame(data)

# select the first column
first_column = df.iloc[:, 0]

# select the first two columns
first_two_columns = df.iloc[:, :2]

# select the last two columns
last_two_columns = df.iloc[:, -2:]

# select specific columns by number
specific_columns = df.iloc[:, [0, 2]]

This will output:

0 John
1 Jane
2 Bob
Name: name, dtype: object

name age
0 John 30
1 Jane 25
2 Bob 40

age city
0 30 New York
1 25 Paris
2 40 London

name city
0 John New York
1 Jane Paris
2 Bob London

In the example above, we first created a sample DataFrame with a ‘name’, ‘age’, and ‘city’ column. We then used the .iloc indexer to select different subsets of columns:

  • df.iloc[:, 0] selects the first column (‘name’).
  • df.iloc[:, :2] selects the first two columns (‘name’ and ‘age’).
  • df.iloc[:, -2:] selects the last two columns (‘age’ and ‘city’).
  • df.iloc[:, [0, 2]] selects the first and third columns (‘name’ and ‘city’).

Note that the .iloc indexer uses zero-based indexing, meaning that the first column has a position of 0, the second column has a position of 1, and so on.


Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post