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.