Home > Mobile >  Pandas: How to change the rows in a dataframe which are found by a query?
Pandas: How to change the rows in a dataframe which are found by a query?

Time:03-04

I want to change values in a dataframe which match a query.

I have a dataframe. I run a query on it. The return-value is another dataframe, with only the lines matched by the query. So the resulting dataframe is a subset of the original.

Example:

>>>f = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

>>>f
    A   B
0   1   3
1   2   4

Now run a query to match only 1 row (the first):

f.query('A==1')
    A   B
0   1   3

How do I do something like:

f.query('A==1')['B']=999

so that the dataframe 'f' becomes

    A   B
0   1   999
1   2   4

You may wonder why I don't use loc. It is my understanding that loc changes the dataframe in-place (which is what I need). I have a function which takes a number of search-parameters, most of them optional. It builds a query from the parameters, before executing it on the dataframe; that's why.

CodePudding user response:

query keep rows so get the returned index by the evaluation of the string expression then use loc to set value:

f.loc[f.query('A==1').index, 'B'] = 999
print(f)

# Output
   A    B
0  1  999
1  2    4
  • Related