Update Pandas df Given String Query

Viewed 598

As a result of data introduced by users in the interface I have a string query-like.

query = '(ColA=="7") & (ColB=="3") & (ColC=="alpha") & (ColD=="yu")'

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

df.loc[query, 'ColZ'] = Z
2 Answers

Yes that can be done with query

df.loc[df.query(query).index,'ColZ']=Z
Related