Ask Your Question
0

What is the total of all values within a dataframe?

asked 2022-03-29 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-05-11 12:00:00 +0000

scrum gravatar image

To find the total of all values within a dataframe in Python pandas, you can use the sum() method, which sums up all the values in each numerical column of the dataframe. For example:

import pandas as pd # create a dataframe with some numerical values df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # get the sum of all values in the dataframe total = df.sum().sum() print(total) # Output: 45 

In this example, we first create a dataframe df with three columns ('A', 'B', 'C') and three rows of numerical values. We then apply the sum() method twice to the dataframe to get the sum of all values. The first sum() method sums up all the values in each column, resulting in a Pandas series object with the column names as index labels. The second sum() method then sums up all the values in the series object, resulting in the total of all values in the dataframe. The output in this example is 45.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-03-29 11:00:00 +0000

Seen: 12 times

Last updated: May 11 '21