pandas replace zero with value in python

You can use the replace() method in pandas to replace zeros with a specific value in a DataFrame. Here's an example:

main.py
import pandas as pd

data = {'A': [1, 2, 0, 4, 0],
        'B': [0, 3, 4, 0, 6]}
df = pd.DataFrame(data)

df.replace(0, 100, inplace=True)  # Replace all zeros with 100

print(df)
180 chars
10 lines

In this example, all zeros in the DataFrame df are replaced with 100. You can replace zeros with any other value by specifying it inside the replace() method.

related categories

gistlibby LogSnag