🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.sum()

AI & DATA SCIENCE // df-sum

df.sum() adds up the values in each column (or, after a groupby(), the values within each group), skipping missing values by default.

Syntax

df.sum(axis=0, skipna=True)

Deep Dive Course

With the default axis=0, sum() adds down each column, producing one total per column as a Series; axis=1 instead sums across each row. By default, skipna=True means NaN values are treated as if they weren't there at all rather than making the whole sum NaN — summing a column containing a NaN alongside real numbers still produces a real number, not NaN, which is usually the desired behavior for real-world data with gaps, but worth knowing explicitly since it's a silent, automatic choice.

1Understanding df.sum()

With the default axis=0, sum() adds down each column, producing one total per column as a Series; axis=1 instead sums across each row. By default, skipna=True means NaN values are treated as if they weren't there at all rather than making the whole sum NaN — summing a column containing a NaN alongside real numbers still produces a real number, not NaN, which is usually the desired behavior for real-world data with gaps, but worth knowing explicitly since it's a silent, automatic choice.

💡

sum()'s default of skipna=True silently ignores NaN values rather than propagating them into the result — if you specifically need to know whether any missing data affected the calculation, check for NaN separately rather than relying on sum() to signal it.

editor.html
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [10, 20, 30]})
print(df.sum())
localhost:3000

2Practical Example

Here is a real-world application of df.sum() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
print(df.groupby("team")["score"].sum())
localhost:3000

3Best Practices

Follow these guidelines when working with df.sum():

1. Chain sum() after groupby() for per-group totals, the pandas equivalent of a SQL GROUP BY with SUM()

2. Be aware of skipna's default of True — NaN values are silently excluded from the sum rather than causing the whole result to become NaN

3. Specify axis explicitly, 0 for column sums, 1 for row sums, rather than relying on remembering the default

⚠️

Tip: sum()'s default of skipna=True silently ignores NaN values rather than propagating them into the result — if you specifically need to know whether any missing data affected the calculation, check for NaN separately rather than relying on sum() to signal it.

editor.html
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [10, 20, 30]})
print(df.sum())
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [10, 20, 30]})
print(df.sum())
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"team": ["A", "A", "B"], "score": [10, 20, 30]})
print(df.groupby("team")["score"].sum())

Best Practices

  • Chain sum() after groupby() for per-group totals, the pandas equivalent of a SQL GROUP BY with SUM()
  • Be aware of skipna's default of True — NaN values are silently excluded from the sum rather than causing the whole result to become NaN
  • Specify axis explicitly, 0 for column sums, 1 for row sums, rather than relying on remembering the default

Interview Question

Why does summing a column containing [1, NaN, 3] give 4 by default, instead of NaN?

Hint: Think about sum()'s skipna parameter and its default value.

sum() defaults to skipna=True, which tells it to simply exclude NaN values from the calculation entirely, treating the sum as if those missing positions weren't part of the data at all, rather than letting a single missing value propagate and make the entire result NaN. This default is usually the more practically useful behavior for real-world data with occasional gaps, but it does mean a column with lots of genuinely missing data can still produce what looks like a normal, complete-looking sum, without any obvious signal that some values were actually skipped — checking isna() separately is necessary if that distinction matters.

Exercises

MediumPractice using df.sum() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3], "b": [10, 20, 30]})
print(df.sum())

Frequently Asked Questions

Why does summing a column containing [1, NaN, 3] give 4 by default, instead of NaN?

sum() defaults to skipna=True, which tells it to simply exclude NaN values from the calculation entirely, treating the sum as if those missing positions weren't part of the data at all, rather than letting a single missing value propagate and make the entire result NaN. This default is usually the more practically useful behavior for real-world data with occasional gaps, but it does mean a column with lots of genuinely missing data can still produce what looks like a normal, complete-looking sum, without any obvious signal that some values were actually skipped — checking isna() separately is necessary if that distinction matters.

Related Functions

df-meandf-countdf-groupby