# Cumulative sum of a column in Pandas – Python

## Cumulative sum of a column in Pandas – Python

Cumulative sum of a column in Pandas can be easily calculated with the use of a pre-defined function cumsum().

Syntax:  cumsum(axis=None, skipna=True, *args, **kwargs)
Parameters:
axis: {index (0), columns (1)}
skipna: Exclude NA/null values. If an entire row/column is NA, the result will be NA
Returns: Cumulative sum of the column

Example 1:

 `import` `pandas as pd` `import` `numpy as np` ` ` `# Create a dataframe` `df1 ``=` `pd.DataFrame({``"A"``:[``2``, ``3``, ``8``, ``14``], ` `                   ``"B"``:[``1``, ``2``, ``4``, ``3``], ` `                   ``"C"``:[``5``, ``3``, ``9``,``2``]}) ` ` ` `# Computing sum over Index axis` `print``(df1.cumsum(axis ``=` `0``))`

Output:

```    A   B   C
0   2   1   5
1   5   3   8
2  13   7  17
3  27  10  19
```

Example 2:

 `import` `pandas as pd` `import` `numpy as np` ` ` `# Create a dataframe` `df1 ``=` `pd.DataFrame({``"A"``:[``None``, ``3``, ``8``, ``14``], ` `                   ``"B"``:[``1``, ``None``, ``4``, ``3``], ` `                   ``"C"``:[``5``, ``3``, ``9``,``None``]}) ` ` ` `# Computing sum over Index axis` `print``(df1.cumsum(axis ``=` `0``, skipna ``=` `True``))`

Output:

```      A    B     C
0   NaN  1.0   5.0
1   3.0  NaN   8.0
2  11.0  5.0  17.0
3  25.0  8.0   NaN
```

Example 3:

 `import` `pandas as pd` `import` `numpy as np` ` ` `# Create a dataframe` `df1 ``=` `pd.DataFrame({``"A"``:[``2``, ``3``, ``8``, ``14``], ` `                   ``"B"``:[``1``, ``2``, ``4``, ``3``], ` `                   ``"C"``:[``5``, ``3``, ``9``,``2``]}) ` ` ` `# Computing sum over Index axis` `print``(df1.cumsum(axis ``=` `1``))`

Output:

```    A   B   C
0   2   3   8
1   3   5   8
2   8  12  21
3  14  17  19
```

Last Updated on October 18, 2021 by admin

## Pandas dataframe.take()Pandas dataframe.take()

Python | Pandas dataframe.take() Python is a great language for doing data analysis, primarily because

## Get the Hour from timestamp in PandasGet the Hour from timestamp in Pandas

Get the Hour from timestamp in Pandas Let’s see how to extract the hour from

## Convert the column type from string to datetime format in Pandas dataframeConvert the column type from string to datetime format in Pandas dataframe

In this tutorial, we will explore how to convert a column of string values to

## Check if a value exists in a DataFrame using in & not in operator in Python-PandasCheck if a value exists in a DataFrame using in & not in operator in Python-Pandas

In this article, Let’s discuss how to check if a given value exists in the

## Python – Pandas str.join() to join string/list elements with passed delimiterPython – Pandas str.join() to join string/list elements with passed delimiter

Python | Pandas str.join() to join string/list elements with passed delimiter Python is a great

## Data Normalization with PandasData Normalization with Pandas

Data Normalization with Pandas In this article, we will learn how to normalize data in

## Return multiple columns using Pandas apply() methodReturn multiple columns using Pandas apply() method

Return multiple columns using Pandas apply() method Objects passed to the pandas.apply() are Series objects whose index

## Delete rows/columns from DataFrame using Pandas.drop()Delete rows/columns from DataFrame using Pandas.drop()

Delete rows/columns from DataFrame using Pandas.drop()   Pandas is one of those packages and makes