我在這里不知所措。任何幫助將不勝感激!
這是我目前的資料。產品從A.1變為A.2。我想要的是當 A.1 變成 A.2 時,我希望我的其余資料只開始計算 A.2 的周期時間。B.1 和 B.3 相同。
這是當前的資料。
--------- ----------- --
| Product | CycleTime | |
--------- ----------- --
| A.1 | 10 | |
--------- ----------- --
| A.1 | 5 | |
--------- ----------- --
| A.1 | 1 | |
--------- ----------- --
| A.1 | 2 | |
--------- ----------- --
| A.1 | 1 | |
--------- ----------- --
| A.2 | 5 | |
--------- ----------- --
| A.2 | 1 | |
--------- ----------- --
| A.1 | 2 | |
--------- ----------- --
| A.1 | 10 | |
--------- ----------- --
| A.2 | 10 | |
--------- ----------- --
| B.1 | 1 | |
--------- ----------- --
| B.1 | 2 | |
--------- ----------- --
| B.1 | 1 | |
--------- ----------- --
| B.3 | 5 | |
--------- ----------- --
| B.1 | 1 | |
--------- ----------- --
| B.3 | 2 | |
--------- ----------- --
| B.1 | 10 | |
--------- ----------- --
這是我正在嘗試制作的內容。
--------- ----------- --
| Product | CycleTime | |
--------- ----------- --
| A.1 | 10 | |
--------- ----------- --
| A.1 | 5 | |
--------- ----------- --
| A.1 | 1 | |
--------- ----------- --
| A.1 | 2 | |
--------- ----------- --
| A.1 | 1 | |
--------- ----------- --
| A.2 | 5 | |
--------- ----------- --
| A.2 | 1 | |
--------- ----------- --
| A.2 | 10 | |
--------- ----------- --
| B.1 | 1 | |
--------- ----------- --
| B.1 | 2 | |
--------- ----------- --
| B.1 | 1 | |
--------- ----------- --
| B.3 | 5 | |
--------- ----------- --
| B.3 | 2 | |
--------- ----------- --
uj5u.com熱心網友回復:
設定:
df = pd.DataFrame(
{
"ProductType":["A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "A", "B", "B", "B"],
"Product":["A.1", "A.1", "A.1", "A.1", "A.1", "A.2", "A.2", "A.1", "A.1", "A.2", "B.1", "B.1", "B.1", "B.3", "B.1", "B.3", "B.1"],
"CycleTime":[10,5,1,2,1,5,1,2,10,10,1,2,1,5,1,2,10],
}
)
在產品之間創建訂單。不同產品型別之間的順序無關緊要,僅在每個產品型別內。即采取幾個部分排序并將它們拼接在一起以創建總排序。您可能必須手動執行此操作,也許您可??以通過編程方式執行此操作,這取決于您的問題。
order = ["A.1", "A.2", "B.1", "B.2", "B.3"]
創建一個地圖,它的反面將每個產品與其在訂單中的位置相關聯。
mapping = dict(enumerate(order))
inversemapping= {v:k for k,v in mapping.items()}
這個想法是,對于每種產品型別,將產品映射到其訂單值,并在映射回產品之前計算這些訂單值的累積最大值。
df.groupby("ProductType").apply(lambda d: d["Product"].map(inversemapping).cummax().map(mapping)).values
您將獲得以下 numpy 陣列:
array(['A.1', 'A.1', 'A.1', 'A.1', 'A.1', 'A.2', 'A.2', 'A.2', 'A.2',
'A.2', 'B.1', 'B.1', 'B.1', 'B.3', 'B.3', 'B.3', 'B.3'],
dtype=object)
uj5u.com熱心網友回復:
如果洗掉重復項并保留第一個值,則下一個產品的索引會顯示無法再找到當前產品的位置:
find_pos = lambda x: x.drop_duplicates('Product')['Product'].shift().dropna() \
.rename_axis('not_valid_after').reset_index()
conds = df.groupby(df['Product'].str.split('.').str[0]) \
.apply(find_pos).reset_index(drop=True)
print(conds)
# Output:
not_valid_after Product
0 5 A.1 # 5 is the first index of A.2
1 13 B.1 # 13 is the first index of B.3
現在您可以過濾資料框:
out = df.drop(conds.apply(lambda x: df.loc[df['Product'] == x['Product']]
.loc[x['not_valid_after']:].index.tolist(),
axis=1).explode().dropna().tolist())
輸出:
>>> out
Product CycleTime
0 A.1 10
1 A.1 5
2 A.1 1
3 A.1 2
4 A.1 1
5 A.2 5
6 A.2 1
9 A.2 10
10 B.1 1
11 B.1 2
12 B.1 1
13 B.3 5
15 B.3 2
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/325864.html
上一篇:Python遞回的區域與全域
