Home > front end >  Post update pandas dataframe .replace function kicking out error
Post update pandas dataframe .replace function kicking out error

Time:05-28

I have some code that used to do exactly what I needed.

port_for_SA_df['Price'] = port_for_SA_df['Price'].replace(0,port_for_SA_df['Invested Amount']
                          /(port_for_SA_df['Units']*product_list_grouped_wo_barriers['Multiplier']))

So that replaced any 0 values in my price column with a formula based on other column values.

After updating to use pandas 1.3.5

and running the same code I get this warning

"Series.replace cannot use dict-value and non-None to_replace"

I've searched online already and cant see what I need to change to get this working again

CodePudding user response:

Try using apply:

port_for_SA_df['Price']  = port_for_SA_df.apply(
    lambda x: x['Price'] if x['Price'] != 0 else x['Invested Amount']/(x['Units']*x['Multiplier']),axis =1 )
  • Related