How to select multiple columns in a pandas dataframe
Last Updated :
30 Nov, 2023
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
In this article, we will discuss all the different ways of selecting multiple columns in a Pandas DataFrame.
Select Multiple Columns in a Pandas Dataframe
Below are the ways by which we can select multiple columns in a Pandas Dataframe:
- Using Basic Method
- Using
loc[]
- Using
iloc[]
- Using
.ix
Select Multiple Columns in a Pandas Dataframe using Basic Method
In this example, we are using basic method that utilizes the Pandas library to create a DataFrame named 'df' from a dictionary of employee data, and then selects and displays only the 'Name' and 'Qualification' columns from the DataFrame.
Python3
# Import pandas package
import pandas as pd
# Define a dictionary containing employee data
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32],
'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# select two columns
df[['Name', 'Qualification']]
Output

Select Second to fourth column
This example uses the pandas library to create a DataFrame 'df' from a dictionary of employee data. It then selects and displays all rows while extracting columns 2 to 4 (Age, Address, and Qualification) using DataFrame slicing based on column indices
Python3
# Import pandas package
import pandas as pd
# Define a dictionary containing employee data
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32],
'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# select all rows
# and second to fourth column
df[df.columns[1:4]]
Output

Select Multiple Columns in a Pandas Dataframe using loc[]
In this example, we are using loc[] function, we are select two columns. In this example, we creates a DataFrame 'df' from a dictionary of employee data. It then selects and displays three rows (index 1 to 3) while extracting specific columns ('Name' and 'Qualification') using the loc
method for label-based indexing.
Python3
# Import pandas package
import pandas as pd
# Define a dictionary containing employee data
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32],
'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# select three rows and two columns
df.loc[1:3, ['Name', 'Qualification']]
Output:

Select one to another columns
In this example, we are using loc[] to select column name "Name" to "Address".
Python3
# Import pandas package
import pandas as pd
# Define a dictionary containing employee data
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32],
'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# select two rows and
# column "name" to "Address"
# Means total three columns
df.loc[0:1, 'Name':'Address']
Output:

This Python code, using the pandas library, creates a DataFrame 'df' from a dictionary of employee data. It then utilizes the .loc
method for label-based indexing to filter and select all columns for the row with index 0, effectively displaying the data for the first employee in the DataFrame.
Python3
# Import pandas package
import pandas as pd
# Define a dictionary containing employee data
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32],
'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
'Qualification':['Msc', 'MA', 'MCA', 'Phd']
}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# .loc DataFrame method
# filtering rows and selecting columns by label
# format
# df.loc[rows, columns]
# row 1, all columns
df.loc[0, :]
Output:

Select Multiple Columns in Pandas using iloc[]
Example 1: Select first two column
In this example, we are using
iloc[]
method for integer-location based indexing to select and display all rows and the first two columns (index 0 and 1) of the DataFrame, adhering to the convention that Python slicing is not inclusive of the ending index.
Python3
# Import pandas package
import pandas as pd
# Define a dictionary containing employee data
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32],
'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# Remember that Python does not
# slice inclusive of the ending index.
# select all rows
# select first two column
df.iloc[:, 0:2]
Output:

Example 2: Select all or some columns, one to another using .iloc
In this example, we are using .iloc
method for integer-location based indexing to select and display rows 0 to 1 and columns 1 to 2 (Age and Address) of the DataFrame.
Python3
# Import pandas package
import pandas as pd
# Define a dictionary containing employee data
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32],
'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# iloc[row slicing, column slicing]
df.iloc [0:2, 1:3]
Output

Select Multiple Columns in a Pandas Dataframe using .ix
Example: Select all or some columns, one to another using .ix
In this example, we are using
.ix
indexer to select and print all rows and columns 0 to 2 (Name and Age) from the DataFrame. It is recommended to use .iloc
for integer-location based indexing instead, as .ix
has been deprecated.
Python3
# Import pandas package
import pandas as pd
# Define a dictionary containing employee data
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32],
'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
# select all rows and 0 to 2 columns
print(df.ix[:, 0:2])
Output
Similar Reads
How to Add Multiple Columns in PySpark Dataframes ? In this article, we will see different ways of adding Multiple Columns in PySpark Dataframes. Let's create a sample dataframe for demonstration: Dataset Used: Cricket_data_set_odi Python3 # import pandas to read json file import pandas as pd # importing module import pyspark # importing sparksessio
2 min read
How to drop one or multiple columns in Pandas DataFrame Let's learn how to drop one or more columns in Pandas DataFrame for data manipulation. Drop Columns Using df.drop() MethodLet's consider an example of the dataset (data) with three columns 'A', 'B', and 'C'. Now, to drop a single column, use the drop() method with the columnâs name.Pythonimport pand
4 min read
How to Select Single Column of a Pandas Dataframe In Pandas, a DataFrame is like a table with rows and columns. Sometimes, we need to extract a single column to analyze or modify specific data. This helps in tasks like filtering, calculations or visualizations. When we select a column, it becomes a Pandas Series, a one-dimensional data structure th
2 min read
How to take column-slices of DataFrame in Pandas? In this article, we will learn how to slice a DataFrame column-wise in Python. DataFrame is a two-dimensional tabular data structure with labeled axes. i.e. columns.Creating Dataframe to slice columnsPython# importing pandas import pandas as pd # Using DataFrame() method from pandas module df1 = pd.
2 min read
Unnest (Explode) Multiple List Columns In A Pandas Dataframe An open-source manipulation tool that is used for handling data is known as Pandas. Have you ever encountered a dataset that has columns with data as a list? In such cases, there is a necessity to split that column into various columns, as Pandas cannot handle such data. In this article, we will dis
6 min read
Split dataframe in Pandas based on values in multiple columns In this article, we are going to see how to divide a dataframe by various methods and based on various parameters using Python. To divide a dataframe into two or more separate dataframes based on the values present in the column we first create a data frame. Creating a DataFrame for demonestrationPy
3 min read
How to Get Column Names in Pandas Dataframe While analyzing the real datasets which are often very huge in size, we might need to get the pandas column names in order to perform certain operations. The simplest way to get column names in Pandas is by using the .columns attribute of a DataFrame. Let's understand with a quick example:Pythonimpo
4 min read
How to Show All Columns of a Pandas DataFrame? Pandas limit the display of rows and columns, making it difficult to view the full data, so let's learn how to show all the columns of Pandas DataFrame. Using pd.set_option to Show All Pandas ColumnsPandas provides a set_option() function that allows you to configure various display options, includi
2 min read
How to widen output display to see more columns in Pandas dataframe? When working with pandas DataFrames, the default print settings often truncate rows, columns, or even the contents of cells. By default only 20 columns are shown. Columns beyond this limit are replaced with ellipses (...) for hidden rows or columns making it difficult to view the entire dataset. To
3 min read
How to Access a Column in a DataFrame with Pandas In this article we will explore various techniques to access a column in a dataframe with pandas with concise explanations and practical examples.Method 1: Accessing a Single Column Using Bracket NotationBracket notation is the most straightforward method to access a column. Use the syntax df['colum
4 min read