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 as pd
data = { 'Name' :[ 'Jai' , 'Princi' , 'Gaurav' , 'Anuj' ],
'Age' :[ 27 , 24 , 22 , 32 ],
'Address' :[ 'Delhi' , 'Kanpur' , 'Allahabad' , 'Kannauj' ],
'Qualification' :[ 'Msc' , 'MA' , 'MCA' , 'Phd' ]}
df = pd.DataFrame(data)
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 as pd
data = { 'Name' :[ 'Jai' , 'Princi' , 'Gaurav' , 'Anuj' ],
'Age' :[ 27 , 24 , 22 , 32 ],
'Address' :[ 'Delhi' , 'Kanpur' , 'Allahabad' , 'Kannauj' ],
'Qualification' :[ 'Msc' , 'MA' , 'MCA' , 'Phd' ]}
df = pd.DataFrame(data)
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 as pd
data = { 'Name' :[ 'Jai' , 'Princi' , 'Gaurav' , 'Anuj' ],
'Age' :[ 27 , 24 , 22 , 32 ],
'Address' :[ 'Delhi' , 'Kanpur' , 'Allahabad' , 'Kannauj' ],
'Qualification' :[ 'Msc' , 'MA' , 'MCA' , 'Phd' ]}
df = pd.DataFrame(data)
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 as pd
data = { 'Name' :[ 'Jai' , 'Princi' , 'Gaurav' , 'Anuj' ],
'Age' :[ 27 , 24 , 22 , 32 ],
'Address' :[ 'Delhi' , 'Kanpur' , 'Allahabad' , 'Kannauj' ],
'Qualification' :[ 'Msc' , 'MA' , 'MCA' , 'Phd' ]}
df = pd.DataFrame(data)
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 as pd
data = { 'Name' :[ 'Jai' , 'Princi' , 'Gaurav' , 'Anuj' ],
'Age' :[ 27 , 24 , 22 , 32 ],
'Address' :[ 'Delhi' , 'Kanpur' , 'Allahabad' , 'Kannauj' ],
'Qualification' :[ 'Msc' , 'MA' , 'MCA' , 'Phd' ]
}
df = pd.DataFrame(data)
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 as pd
data = { 'Name' :[ 'Jai' , 'Princi' , 'Gaurav' , 'Anuj' ],
'Age' :[ 27 , 24 , 22 , 32 ],
'Address' :[ 'Delhi' , 'Kanpur' , 'Allahabad' , 'Kannauj' ],
'Qualification' :[ 'Msc' , 'MA' , 'MCA' , 'Phd' ]}
df = pd.DataFrame(data)
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 as pd
data = { 'Name' :[ 'Jai' , 'Princi' , 'Gaurav' , 'Anuj' ],
'Age' :[ 27 , 24 , 22 , 32 ],
'Address' :[ 'Delhi' , 'Kanpur' , 'Allahabad' , 'Kannauj' ],
'Qualification' :[ 'Msc' , 'MA' , 'MCA' , 'Phd' ]}
df = pd.DataFrame(data)
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 as pd
data = { 'Name' :[ 'Jai' , 'Princi' , 'Gaurav' , 'Anuj' ],
'Age' :[ 27 , 24 , 22 , 32 ],
'Address' :[ 'Delhi' , 'Kanpur' , 'Allahabad' , 'Kannauj' ],
'Qualification' :[ 'Msc' , 'MA' , 'MCA' , 'Phd' ]}
df = pd.DataFrame(data)
print (df.ix[:, 0 : 2 ])
|
Output
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...