Home > Software design >  Using complex operations in query method of pandas
Using complex operations in query method of pandas

Time:05-14

Let say I have below code,

import pandas as pd
dat = pd.DataFrame({'col1' : [1,2,3,4,5,6,7,8,9,10], 'col2' : ['A', 'X', 'D', 'Y', 'A', 'D', 'Y', 'X', 'D', 'A']})
dat1 = pd.DataFrame({'col1' : [1,2,3,4,5,6,7,8,9,10], 'col2' : ['A', 'X', 'D', 'Y', 'A', 'D', 'Y', 'X', 'D', 'A']})
number = dat['col1'].values[dat.shape[0] - 3]
dat1.query("col1 == @number")

now instead of using @number in the last line, I want pass entire calculation dat['col1'].values[dat.shape[0] - 3] in the query method.

Is there any way to achieve this?

CodePudding user response:

What exactly is your use case? query does not support such complex expression.

You can however use loc with a callable, which enables you to use a dynamic expression in a pipeline:

dat1.loc[lambda d: d['col1'].eq(d['col1'].values[d.shape[0] - 3])]

output:

   col1 col2
7     8    X
  • Related