As a result of data introduced by users in the interface I have a string query-like.
JavaScript
x
2
1
query = '(ColA=="7") & (ColB=="3") & (ColC=="alpha") & (ColD=="yu")'
2
Now I want to update a column of the df based on those conditions, assigning it a variable Z.
I don’t know if it can be done somehow with loc
JavaScript
1
2
1
df.loc[query, 'ColZ'] = Z
2
Advertisement
Answer
I got the solution by myself:
JavaScript
1
2
1
df.loc[df.eval(query), 'ColZ'] = Z
2
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.eval.html