6

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
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
burn1000
  • 91
  • 6

2 Answers2

5

Yes that can be done with query

df.loc[df.query(query).index,'ColZ']=Z
Attila the Fun
  • 327
  • 2
  • 13
BENY
  • 317,841
  • 20
  • 164
  • 234
0

I got the solution by myself:

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

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.eval.html

burn1000
  • 91
  • 6