Pandas: Replace zeros with previous non zero value

Given a pandas dataframe, we have to replace zeros with previous non zero value.
Submitted by Pranit Sharma, on December 02, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

Suppose we have a data frame with multiple columns and one of its columns containing some integer values. It is noted that there are a lot of zeros in this column and we need to fill these zeros with the other non-zero values in the same column.

Replacing zeros with previous non zero value

For this purpose, we will use replace() method and pass a parameter method = ffill. This method is highly used when we need to replace a regex, list, string, series, dictionary, number, etc from a Pandas data frame column.

Syntax:

DataFrame.replace(
    to_replace=None, 
    value=None, 
    inplace=False, 
    limit=None, 
    regex=False, 
    method='pad',
    axis=None)

Let us understand with the help of an example,

Python program to replace zeros with previous non zero value

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d = {'a':[1,0,0,2,4,
          0,6,0,3,2,0,
          5,2,0,5,0,
          2,4,0]}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display Original df
print("Original DataFrame:\n",df,"\n")

# Filling 0s with previous non zeroes
res = df['a'].replace(to_replace=0, method='ffill')

# Display result
print("Result:\n",res)

Output

The output of the above program is:

Example: Pandas: Replace zeros with previous non zero value

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.