我看到了一些有关在like this one范围内查找单个值的问题,但是我需要遍历所有行并且性能更高的东西。
# I have some dataset (10k to 1m rows)
values = pd.DataFrame([["foo", 5], ["bar", 15]], columns=["foobar", "values"])
# and a lookup table (25 rows)
lookups = pd.DataFrame([["A1", 0, 10], ["A2", 10, 20]], columns=["tier", "min", "max"])
我希望得出的结果是根据值的值以及在查找表的最小和最大范围之间查找层:
foobar values tier
0 foo 5 A1
1 bar 15 A2
我有一些工作要做,但是扩展性很差:
def lookup(score):
for idx, row in lookups.iterrows():
if row["min"] <= score < row["max"]:
return row["tier"]
values["tier"] = values["values"].apply(lookup)
我的第二个想法是创建一个索引为(0-lookup.max.max()]且重复/平铺的数据帧,但希望有更多内置选项吗?
谢谢
答案 0 :(得分:2)
这是var operation = TableOperation.Retrieve<Entity>(partitionKey, id, new List<string> { "Content", "IsDeleted" });
:
pd.cut
输出:
values['tier'] = pd.cut(values['values'],
bins=list(lookups['min']) + [lookups['max'].iloc[-1]],
labels=lookups['tier']
)