深度阅读

How to calculate the sum of a column in a Pandas DataFrame?

作者
作者
2023年08月22日
更新时间
11.41 分钟
阅读时间
0
阅读量

To calculate the sum of a column in a Pandas DataFrame, you can use the sum() function. Here’s an example:

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # create a DataFrame with columns A and B
sum_col = df['B'].sum() # calculate the sum of column 'B'
print(sum_col)

Output:

15

In the example above, sum_col stores the sum of values in column ‘B’ of the DataFrame. The sum() function calculates the sum of values in a column, so in this case the sum of values in column ‘B’ is 15.

You can also calculate the sum of values across multiple columns by calling the sum() function on the entire DataFrame:

sum_all = df.sum().sum() # calculate the sum of all values in the DataFrame
print(sum_all)

Output:

21

In this example, sum_all stores the sum of all values in the DataFrame. The first call to sum() calculates the sum of values in each column, and the second call to sum() calculates the sum of values across all columns.

Overall, calculating the sum of a column or all values in a Pandas DataFrame is straightforward using the sum() function.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。