I would like to change values in a dataframe using np.select However, I'm getting an error and google didn't find any solution for similar issues. Could somebody help me out? Thank you in advance for your time and help. Here is my dataframe
import pandas as pd
import numpy as np
df = pd.DataFrame({'A':[1,0,-0.6,-1,0.7],
'B':[-1,1,-0.3,0.5,1]})
df
Then, this my code where df values are recoded as follow:
values less than or equal to -0.5 become -1
values greater than -0.5 and less than or equal to 0.5 become 0
values greater than 0.5 become 1
new_df = pd.DataFrame( np.select([df.le(-0.5), df.gt(-.5) & df.le(0.5), df.gt(0.5)], [-1, 0, 1]), index=df.index, columns=df.columns )
new_df
My code gave me the following error and I'm not sure what's wrong and how to fix it
KeyError: <function dispatch_to_series..column_op at 0x7f64a469d158>