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
How to select multiple columns in a pandas dataframe
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.