Pandas Select Columns by Index
One of the most important aspects of data analysis is to select the required data from the dataset. This helps us to analyze the data in a better way. It is a way to filter out the data we don't need and work with the data we need.
To do this, we need to select the columns that we want to work with. In this tutorial, we will learn how to select columns by index in pandas.
- Select Single Column by Index
- Select Multiple Columns by Index
- Select Columns by Index in Range
- Conclusion
Table of Contents
1. Select Single Column by Index
To select columns by index, we use the iloc()
function on the DataFrame object.
The iloc()
function takes two parameters, the row index and the column index. If we want to select all the rows, we can pass :
as the row index.
Here is an example of selecting a single column by index.
import pandas as pd
# Creating a sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Emma'],
'Age': [25, 30, 35, 40, 45],
'City': ['NY', 'LA', 'SF', 'NY', 'LA']}
df = pd.DataFrame(data)
# Selecting columns by column index
# Selecting the first column (index = 0)
print(df.iloc[:, 0])
Output:
0 Alice 1 Bob 2 Charlie 3 David 4 Emma Name: Name, dtype: object
2. Select Multiple Columns by Index
To select multiple columns by index, pass a list of column indexes you want to select to the iloc()
function.
If you want to select the second and third columns, pass [1, 2]
as the column index.
import pandas as pd
# Creating a sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Emma'],
'Age': [25, 30, 35, 40, 45],
'City': ['NY', 'LA', 'SF', 'NY', 'LA']}
df = pd.DataFrame(data)
# Selecting columns by column index
# Selecting the second and third column (index = 1, 2)
print(df.iloc[:, [1, 2]])
Output:
Age City 0 25 NY 1 30 LA 2 35 SF 3 40 NY 4 45 LA
3. Select Columns by Index in Range
Let your dataframe has 10 columns and you want to select columns from 3 to 7. You can do this by passing 3:8
as the column index, i.e. start_index:end_index+1
.
Here is an example of selecting columns in range by index.
import pandas as pd
# Creating a sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Emma'],
'Age': [25, 30, 35, 40, 45],
'City': ['NY', 'LA', 'SF', 'NY', 'LA']}
df = pd.DataFrame(data)
# Selecting columns by column index
# Selecting the columns from index 0 to 2
print(df.iloc[:, 0:3])
Output:
Name Age City 0 Alice 25 NY 1 Bob 30 LA 2 Charlie 35 SF 3 David 40 NY 4 Emma 45 LA
Conclusion
You can now select columns by index in pandas with different variations. You can select a single column, multiple columns or columns in range by index.
Learn how to select columns by name in pandas.