Home > Blockchain >  pandas replace 0s with a value returned by a function
pandas replace 0s with a value returned by a function

Time:11-16

I have a bunch of zeros in my pandas dataframe. I'd like to replace them with a value returned by a function. I am doing something similar using fillna. Here's a simplified version of my code for reproducibility:

import pandas as pd
import numpy as np

def replace(x):
    v = x * 2
    return v

df = pd.DataFrame({'col1': [1,np.nan,0],
                   'col2': [1,2,3]
                 })

df['col1'].replace(0, value returned by replace function)

Ideally, I'd like use lambda to do this. Here's how I am doing this for NaNs:

df['col1'].fillna(df.apply(lambda x: replace(x.col2), axis=1), inplace=True)

CodePudding user response:

apply can take the lambda function that you wish to use:

df['col1'] = df.apply(lambda x: x['col2']*2 if x['col1'] == 0 else x['col1'], axis=1)

CodePudding user response:

In general, if you want to replace a specific value in a DataFrame with that returned by a simple function, you can use pd.DataFrame.where().

To replace all 0s in a DataFrame with the result of replace():

new_df = df.where( df != 0, replace( df ) )

Wherever the condition is true (in this case, df is not equal to 0), the function keeps the original value, and wherever the condition is false (in this case, df is equal to 0), the function returns the result of replace().

The where() function can also be applied to an individual series. To use your function:

df["col1"] = df["col1"].where( df["col1"] != 0, replace( df["col1"] ) )

To replace values of col2 with replace() where col1 is equal to zero:

df["col2"] = df["col2"].where( df["col1"] != 0, replace( df["col2"] ) )
  • Related