Python 在数据帧中满足特定条件后,选择连续行

Python 在数据帧中满足特定条件后,选择连续行,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,满足条件后,选择连续行。例如: dfA : style day stock pants mon 0 pants tue 2 pants wed 1 pants thu 0 pants fri 1 pants sat 0 shirt sat 1 shirt thu 2 shirt mon 0 shirt tue 0 shirt wed 2 shirt fri 0 在订购天数后以及库存>0时,选择连续行 expecte

满足条件后,选择连续行。例如:

dfA : 
style  day  stock
pants  mon   0
pants  tue   2
pants  wed   1
pants  thu   0
pants  fri   1
pants  sat   0
shirt  sat   1
shirt  thu   2
shirt  mon   0
shirt  tue   0
shirt  wed   2
shirt  fri   0

在订购天数后以及库存>0时,选择连续行

expected output :
pants  tue   2
pants  wed   1
pants  thu   0
pants  fri   1
pants  sat   0
shirt  sat   1
shirt  thu   0
shirt  wed   2
shirt  fri   0
因此,在裤子中,当库存在周二达到2时,我们选择之后的所有日子;在衬衫中,当库存在周三达到2时,我们选择之后的所有日子。
我尝试按和>0条件分组,但没有得到预期的结果,任何潜在客户都会很感激。

这里有一个解决方案,它可能不是最优雅的,但它的优点是非常清晰

在这里,我将介绍带有日期id的中间列(按正确的周顺序!),以及每个样式的第一天的库存。然后您只需要比较这两列

week = ["mon", "tue", "wed", "thu", "fri", "sat"]
fun = lambda x: week.index(x)
a["day_id"] = a["day"].map(fun)   # column with day id
first_day = a[a["stock"] > 0].groupby("style")["day_id"].min()   # select lines with stock>0 and find first day per style
a["first_day"] = a["style"].map(first_day)   # column with first day with stock per style (very redundant)
result = a[a["day_id"] >= a["first_day"]]

您可以使用以下方法应用分组。 创建临时dict以保存生成的分组dfs:

dict_temp = {key: None for key in df["style"].unique()}
满足条件后获取其余行:

for key in dict_temp:
    # get the index of first True of condition (here ==2)
    index_fist_true = np.where(df.groupby("style").get_group(key)["stock"]==2)[0][0]
    # save the rest of grouped dataframe in dict_temp
    dict_temp[key] = df.groupby("style").get_group(key).reset_index()[index_fist_true+1:]
浓缩dict_temp的值并进行转换:

pd.concat(dict_temp.values()).drop("index", axis=1).reset_index(drop=True)

请添加预期输出结果数据框是预期输出您如何进行“天数排序”?