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.
Let’s discuss all different ways of selecting multiple columns in a pandas DataFrame.
Method #1: Basic Method
Given a dictionary which contains Employee entity as keys and list of those entity as values.
# 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.
# 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:

Method #2: Using loc[]
Example 1: Select two columns
# 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:

Example 2: Select one to another columns. In our case we select column name “Name” to “Address”.
# 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:

Example 3: First filtering rows and selecting columns by label format and then Select all columns.
# 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:

Method #3: Using iloc[]
Example 1: Select first two column.
# 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.
# 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:

Method #4: Using .ix
Select all or some columns, one to another using .ix.
# 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:

Recommended Posts:
- How to drop one or multiple columns in Pandas Dataframe
- How to rename columns in Pandas DataFrame
- Difference of two columns in Pandas dataframe
- Python | Pandas DataFrame.columns
- Getting frequency counts of a columns in Pandas DataFrame
- Dealing with Rows and Columns in Pandas DataFrame
- Iterating over rows and columns in Pandas DataFrame
- Conditional operation on Pandas DataFrame columns
- Collapse multiple Columns in Pandas
- Split a text column into two columns in Pandas DataFrame
- Split a String into columns using regex in pandas DataFrame
- Using dictionary to remap values in Pandas DataFrame columns
- Change Data Type for one or more columns in Pandas Dataframe
- Select row with maximum and minimum value in Pandas dataframe
- How to randomly select rows from Pandas DataFrame
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



