Pandas
DataFrame
Python
Data Analysis
Summation

Pandas sum DataFrame rows for given columns

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Summing selected columns row by row is a very common pandas operation. It appears in scoring models, reporting tables, feature engineering, and data-cleaning pipelines. The mechanics are simple, but correct results depend on choosing the right columns, using the right axis, and deciding how missing values should behave.

Sum Specific Columns Row-Wise

The basic pattern is to select the columns you want and call sum(axis=1). The axis=1 part is essential because it tells pandas to sum across columns for each row.

python
1import pandas as pd
2
3scores = pd.DataFrame(
4    {
5        "student": ["Ana", "Ben", "Cara"],
6        "math": [78, 91, 85],
7        "science": [88, 95, 79],
8        "english": [81, 87, 90],
9        "absences": [1, 0, 2],
10    }
11)
12
13score_cols = ["math", "science", "english"]
14scores["total_score"] = scores[score_cols].sum(axis=1)
15print(scores)

This keeps the original columns and adds a derived total. It is the right approach when the set of columns is known and stable.

Selecting Columns Dynamically

Hardcoding every column is not always practical. In wide tables, it is often better to derive the list from a naming rule.

python
1sales = pd.DataFrame(
2    {
3        "order_id": [1, 2, 3],
4        "amt_q1": [1200, 1400, 900],
5        "amt_q2": [1100, 1600, 950],
6        "amt_q3": [1300, 1500, 1000],
7        "region": ["East", "West", "East"],
8    }
9)
10
11amount_cols = [col for col in sales.columns if col.startswith("amt_")]
12sales["annual_amount"] = sales[amount_cols].sum(axis=1)
13print(sales)

This is useful when upstream schemas change or when the column group is defined by metadata rather than by a fixed hand-written list.

Handling Missing Values Intentionally

By default, pandas skips missing values when summing. That is often convenient, but it is not always correct for the business rule.

python
1expenses = pd.DataFrame(
2    {
3        "rent": [1200, 1400, None],
4        "food": [400, None, 380],
5        "transport": [120, 95, 110],
6    }
7)
8
9expenses["skip_missing"] = expenses[["rent", "food", "transport"]].sum(axis=1)
10expenses["require_all_values"] = expenses[["rent", "food", "transport"]].sum(
11    axis=1,
12    min_count=3,
13)
14print(expenses)

The skip_missing column still produces totals when one input is missing. The min_count=3 version returns a missing result unless all three values are present. Choose the rule explicitly. Otherwise your totals may look valid while hiding incomplete records.

Fixing Data Types Before Summing

Another common problem is mixed types. Numeric-looking values sometimes arrive as strings, especially after CSV imports or poorly typed JSON ingestion. Convert them before summing.

python
1raw = pd.DataFrame(
2    {
3        "a": ["10", "20", "30"],
4        "b": ["3", "bad", "7"],
5    }
6)
7
8raw[["a", "b"]] = raw[["a", "b"]].apply(pd.to_numeric, errors="coerce")
9raw["row_sum"] = raw[["a", "b"]].sum(axis=1)
10print(raw)

Using errors="coerce" turns invalid numeric values into missing values instead of throwing immediately. That can be useful in cleaning pipelines, but it should be paired with validation so bad source data does not pass unnoticed.

Weighted or Conditional Row Sums

Sometimes a row total is not a plain arithmetic sum. You may need weights or conditions.

python
weights = pd.Series({"math": 0.4, "science": 0.35, "english": 0.25})
scores["weighted_score"] = scores[weights.index].mul(weights, axis=1).sum(axis=1)
print(scores[["student", "weighted_score"]])

Or you may want to sum only positive values:

python
values = pd.DataFrame({"x": [3, -1, 2], "y": [5, 4, -2], "z": [1, -3, 6]})
values["positive_sum"] = values[["x", "y", "z"]].clip(lower=0).sum(axis=1)
print(values)

These patterns are common in scoring systems and business rules where not every column contributes equally.

Common Pitfalls

The most common mistake is forgetting axis=1. Without it, sum aggregates down each column instead of across each row.

Another issue is accidentally including non-numeric columns in the selection set. That may fail outright or produce inconsistent behavior depending on dtypes.

Missing-value behavior is another source of confusion. The default skip behavior is convenient, but it may hide incomplete input. If all values are required, use min_count or validate before summing.

Finally, avoid writing row-wise Python loops for this task. pandas column operations are clearer and usually much faster than iterating through rows manually.

Summary

  • Use df[columns].sum(axis=1) to sum selected columns per row.
  • Build the column list dynamically when schema rules are pattern-based.
  • Decide whether missing values should be skipped or should invalidate the result.
  • Convert string-like numeric data before aggregation.
  • Prefer vectorized pandas operations over manual row loops.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.