目标:我希望定义一个函数,它接受一个参数 - 列名和值的字典 - 并从Pandas数据框中返回匹配条件列表
详细信息:我希望以编程方式生成以下字符串
data[(mydf.anchor_name == 'ing') & (mydf.sales_qty ==8)]
生成示例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']])
})
我一直在尝试使用此代码:
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)+")]"