Objective: I am looking to define a function which takes a single argument - a dictionary of column names and values - and returns a list of matching criteria from a Pandas data frame
Details: I am looking to programmatically generate the following string
data[(mydf.anchor_name == 'ing') & (mydf.sales_qty ==8)]
Generates an example pandas dataframe:
import pandas as pd
mydf = pd.DataFrame({'sales_qty' : pd.Series([4,8,10]),
'distance' : pd.Series([454.75,477.25,242.12]),
'signature' : pd.Series(['ab','cd','ab']),
'anchor_name' : pd.Series(['tec','ing','pol']),
'station_list' : pd.Series([['t1','t2','t3'],
['4','t2','t3'],['t3','t2','t4']])
})
I have been trying to work with this code:
data = mydf
params = {"anchor_name": 'ing', "sales_qty": 8}
filters = ["{}".format(k) for k in params]
t = tuple(params.values())
data += "[df."+ " ) & (df.".join(t).join(filters)+")]"