Pandas How To Uncategorized How to sum a column

How to sum a column

In this post you learn how to sum a column in Pandas library.

Sum calculations for a column

To sum the values of a column in a pandas DataFrame, you can use the sum method:

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

result = df['A'].sum()

You can also use the agg method to perform multiple aggregation functions on multiple columns at once:

result = df.agg({'A': 'sum', 'B': 'sum'})

In this example, the agg method returns a Series with the sum of values in each column:

A     6
B    15
dtype: int64

See also:
How to calculate sum of rows and columns in Pandas
How to calculate cumulative sum in Pandas
How to add a column

Tags: ,

Leave a Reply

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

Related Post