JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

How to get the sum of elements in a Pandas column

Author:JIYIK Last Updated:2025/05/01 Views:

We will show you how to get the sum of the elements of a Pandas DataFrame column, as well as groupbymethods for calculating cumulative sums and methods for summing columns based on conditions on other column values.


How to get the DataFramesum of a Pandas column

First, we NumPycreate a random array using the library and then use sum()the function to get the sum of each column.

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randint(0, 10, size=(10, 4)), columns=list("1234"))
print(df)
Total = df["1"].sum()
print("Column 1 sum:", Total)
Total = df["2"].sum()
print("Column 2 sum:", Total)
Total = df["3"].sum()
print("Column 3 sum:", Total)
Total = df["4"].sum()
print("Column 4 sum:", Total)

If you run this code, you will get the following output (values ​​may be different in your case),

   1  2  3  4
0  2  2  3  8
1  9  4  3  1
2  8  5  6  0
3  9  5  7  4
4  2  7  3  7
5  9  4  1  3
6  6  7  7  3
7  0  4  2  8
8  0  6  6  4
9  5  8  7  2
Column 1 sum: 50
Column 2 sum: 52
Column 3 sum: 45
Column 4 sum: 40

groupbyThe cumulative sum of

We can use groupbythe method to get the cumulative sum. Consider the following with DataFrame, , Fruitand Salecolumns DataFrame:

import pandas as pd

df = pd.DataFrame(
    {
        "Date": ["08/09/2018", "10/09/2018", "08/09/2018", "10/09/2018"],
        "Fruit": ["Apple", "Apple", "Banana", "Banana"],
        "Sale": [34, 12, 22, 27],
    }
)

If we want to calculate the cumulative sales of each fruit, for each date we can calculate it like this,

import pandas as pd

df = pd.DataFrame(
    {
        "Date": ["08/09/2018", "10/09/2018", "08/09/2018", "10/09/2018"],
        "Fruit": ["Apple", "Apple", "Banana", "Banana"],
        "Sale": [34, 12, 22, 27],
    }
)

print(df.groupby(by=["Fruit", "Date"]).sum().groupby(level=[0]).cumsum())

After running the above code, we will get the following output, which shows the cumulative sum of fruits for each date:

Fruit  Date         Sale
Apple  08/09/2018    34
       10/09/2018    46
Banana 08/09/2018    22
       10/09/2018    49
        

How to get the sum of a column based on other column values

This method provides Truethe functionality to get the sum when a given condition is , and Falseto replace the sum with a given value when a condition is . Consider the following code

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randn(5, 3), columns=list("xyz"))

df["sum"] = df.loc[df["x"] > 0, ["x", "y"]].sum(axis=1)

df["sum"].fillna(0, inplace=True)
print(df)

In the above code, we add a new column sum DataFrameto , which is ['x','y']the sum of the first column , if ['x']is greater than 1, otherwise we replace the sum with 0.

After running the code, we will get the following output (values ​​may change depending on your case).

          x         y         z       sum
0 -1.067619  1.053494  0.179490  0.000000
1 -0.349935  0.531465 -1.350914  0.000000
2 -1.650904  1.534314  1.773287  0.000000
3  2.486195  0.800890 -0.132991  3.287085
4  1.581747 -0.667217 -0.182038  0.914530

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert Pandas to CSV without index

Publish Date:2025/05/01 Views:159 Category:Python

As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates

Convert Pandas DataFrame to Dictionary

Publish Date:2025/05/01 Views:197 Category:Python

This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan

Convert Pandas DataFrame columns to lists

Publish Date:2025/05/01 Views:191 Category:Python

When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m

Subtracting Two Columns in Pandas DataFrame

Publish Date:2025/05/01 Views:120 Category:Python

Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi

Dropping columns by index in Pandas DataFrame

Publish Date:2025/05/01 Views:99 Category:Python

DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop() the method to delete columns

Pandas Copy DataFrame

Publish Date:2025/05/01 Views:53 Category:Python

This tutorial will show you how to DataFrame.copy() copy a DataFrame object using the copy method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 ], "Cost" : [ "300" , "400" , "350" ], } ) print (items_df) Output:

Pandas DataFrame.ix[] Function

Publish Date:2025/05/01 Views:168 Category:Python

Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument. pandas.DataFrame.ix[] grammar DataFrame . ix[index = None , label = None ] parameter index Integer or list of integers used to slice row indice

Pandas DataFrame.describe() Function

Publish Date:2025/05/01 Views:120 Category:Python

Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc

Pandas DataFrame.astype() Function

Publish Date:2025/05/01 Views:160 Category:Python

Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial