• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Sum Columns Dynamically with pandas in Python

Sum Columns Dynamically with pandas in Python

August 27, 2022 Leave a Comment

To sum columns dynamically, you can create a list containing the columns you want and use the sum() function, passing “axis=1”.

import pandas as pd

df = pd.read_csv("some_data.csv")

cols_to_sum = ["col1","col2"]

df["Total"] = df[cols_to_sum].sum(axis=1)

When working with collections of data, the ability to aggregate different pieces of data in different ways easily is valuable.

One such case is if you have a dataset and want to create new columns from the sum of other columns.

You can sum columns dynamically with pandas easily.

First, you build a list containing the column names you want to sum and then you can use the sum() function. By default, sum() sums the rows, and so you need to pass “axis=1” to sum over the columns.

Below is a simple example showing you how to sum varying columns in Python with pandas.

import pandas as pd

df = pd.read_csv("some_data.csv")

cols_to_sum = ["col1","col2"]

df["Total"] = df[cols_to_sum].sum(axis=1)

This is useful if you have a loop and want to create different sums based on differing columns depending on certain conditions.

import pandas as pd

df = pd.read_csv("some_data.csv")

for x in range(0,10):
    cols_to_sum = ["col" + str(x),"col2" + str(x + 1)]
    df["Total" + str(x)] = df[cols_to_sum].sum(axis=1)

Other Functions to Use to Aggregate Columns Dynamically with pandas

You can use the same structure from above to aggregate your data with different calculations.

For example, you could take the max over different columns with the pandas DataFrame max() function in the following way.

import pandas as pd

df = pd.read_csv("some_data.csv")

cols = ["col1","col2"]

df["Max"] = df[cols].max(axis=1)

If you wanted to get the mean of multiple columns in pandas, you could use the pandas DataFrame mean() function as shown below.

import pandas as pd

df = pd.read_csv("some_data.csv")

cols = ["col1","col2"]

df["Mean"] = df[cols].mean(axis=1)

There are many great functions which pandas offers us and you can explore them in the documentation here.

Hopefully this article has been useful for you to learn how to sum columns dynamically with pandas in Python.

Other Articles You'll Also Like:

  • 1.  Read Last Line of File Using Python
  • 2.  Divide Each Element in List by Scalar Value with Python
  • 3.  Mastering Data Concatenation with Pandas
  • 4.  How to Output XLSX File from Pandas to Remote Server Using Paramiko FTP
  • 5.  Using Python to Sort Two Lists Together
  • 6.  Get Year from Date in Python
  • 7.  Mastering Multiplication in Python: A Comprehensive Guide
  • 8.  How to Create Array from 1 to n in Python
  • 9.  Python Add Days to Date Using datetime timedelta() Function
  • 10.  pi in Python – Using Math Module and Leibniz Formula to Get Value of pi

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy