Sum of all the columns of a pandas dataframe with a wildcard name search

Given a pandas dataframe, we have to find the sum of all the columns with a wildcard name search. By Pranit Sharma Last updated : October 03, 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

We are given a DataFrame with multiple columns like c1a, c1b, c2a, c2b, etc, we need to find some of those columns which contain c2 in their name.

Finding the sum of all the columns with a wildcard name search

For this purpose, we will filter out the column names with the help of str.startswith() method inside which we will pass a particular substring based on which we need to filter out our columns. Once the columns are filtered, we will then find the sum of all those columns using the sum() method.

Let us understand with the help of an example,

Python program to find the sum of all the columns with a wildcard name search

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d = {'c1a':[1,2,3],'c2a':[1,2,3],'c1b':[4,5,6],'c2b':[4,5,6]}

# Creating a DataFrame
df = pd.DataFrame(d)

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

# Filtering out columns and finding the sum
res = df.loc[:, [x for x in df.columns if x.startswith('c1')]].sum(axis=1)

# Display Result
print('Result:\n',res)

Output

The output of the above program is:

Example: Sum of all the columns of a pandas dataframe with a wildcard name search

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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