I am trying to use the .where pandas dataframe method, only I have more than 2 possibilities (ie i have if, elif, else instead of the default behavior if else)
Please consider the following dataframe:
a1 = np.random.rand(7,2)
a2 = np.random.randint(0,3,(7,1))
grid = np.append(a1, a2, axis=1)
df = pd.DataFrame(grid)
I tried
def test(x):
if x[2] == 0:
return 5
if x[2]==1:
return 10
if x[2] ==2:
return 50
df.where(test)
But I receive error message "truth value of a serie is ambiguous". I suspect this is the right direction but I am confused on how to achieve it. The documentation says that if the condition is a callable, the input is considered to be the full df. However even then it seems that it consider x[2] as the entire column 2. Is there no way to achieve a vectorized operation for that task? Is it only possible to iterate row by row, whether with iterrows or apply?
This is a toy example to be clear on the forum, I am not trying to do a simple .map in my real life problem. Please keep the "test" function as a separate function that needs to be passed if you answer, as this is where my difficulty lies.