Selecting columns by list where columns are subset of list

Given a pandas dataframe, we have to select columns by list where columns are subset of list. By Pranit Sharma Last updated : October 02, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

Suppose, we are given a DataFrame with several columns, this DataFrame has a list of column names. If all elements of the list are in DataFrame, everything works fine but if some elements of the list are not in DataFrame, it will produce an error.

How to select columns by list where columns are subset of list?

We need to select all columns which are included in the list, even if not all elements of the list are included in the DataFrame. For this purpose, we will use the intersection() method on the columns of DataFrame inside which we will pass that list containing column names and some other elements. Only those in the DataFrame will be calculated.

Let us understand with the help of an example,

Python program to select columns by list where columns are subset of list

# Importing pandas package
import pandas as pd

# Creating two dictionaries
d = {
    'A':[1,2,3],
    'B':[4,5,6],
    'C':[7,8,9],
    'D':[1,3,5],
    'E':[5,3,6],
    'F':[7,4,3]
}

# Creating DataFrame
df = pd.DataFrame(d)

# Display Original DataFrame
print("Created DataFrame:\n",df,"\n")

# Creating a list of elements similar 
# to column names
l = ['A','R','C','F']

# Getting columns
res = df[df.columns.intersection(l)]

# Display result
print("Result:\n",res)

Output

The output of the above program is:

Example: Selecting columns by list

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.