我有一個如下所示的資料框
stud_id prod_id total_qty ques_date inv_qty inv_date bkl_qty bkl_date csum accu_qty accu_date upto_inv_threshold upto_bkl_threshold upto_accu_threshold
0 101 12 100 13/11/2010 7.00000 16/02/2012 15 2013-01-16 15 10 13/08/2021 7.00000 22.00000 32.00000
1 101 12 100 13/11/2010 7.00000 16/02/2012 40 2011-10-22 55 10 13/08/2021 7.00000 62.00000 72.00000
2 101 12 100 13/11/2010 7.00000 16/02/2012 2 2019-09-10 57 10 13/08/2021 7.00000 64.00000 74.00000
df = pd.read_clipboard()
我想做下面列出的兩個步驟
步驟 1)在資料框中搜索 >=50 的值并僅回傳第一次出現。
僅在 3 列中進行上述搜索 - upto_inv_threshold, upto_bkl_threshold,upto_accu_threshold但按列進行。意思是先完成一列中的搜索,然后移動到下一列。例如:我們upto_inv_threshold首先搜索 的所有值,然后搜索 的所有值upto_bkl_threshold,然后/最后搜索 的所有值upto_accu_threshold
step-2) 獲取在步驟 1 中找到的第一個出現值的對應日期。如果找到該值upto_inv_threshold,則獲取inv_date. 如果在 中找到第一次出現的值upto_bkl_threshold,則獲取bkl_date. 如果在 中找到第一次出現的值upto_accu_threshold,則獲取accu_date.
我嘗試了以下
df_stage_3.loc[:, 'upto_inv_threshold':'upto_accu_threshold']
np.where(df_stage_3.loc[:, 'upto_inv_threshold':'upto_accu_threshold']>=50)
但這無處可去,我無法進一步進行。
我們必須為每個stud_id和執行此操作prod_id。目前,在樣本資料中,我們只有組,但在實際資料中,我們將有多個stud_id和組prod_id。
我希望我的輸出如下所示。我們從bkl_date列中獲取日期,因為第一個值(滿足我們的標準 >=50)是 62(存在于 upto_bkl_threshold 中)
stud_id, prod_id, fifty_pct_date
101, 12, 2011-10-22
uj5u.com熱心網友回復:
選擇 required cols,然后創建一個布林值mask來識別閾值中的單元格,例如值 > 50 的列,然后使用此布爾掩碼來屏蔽相應日期列中的值。現在group資料框由stud_id和prod_id聚合使用first,最后bfill(回填)沿列軸獲得第一次出現的日期達到閾值。
cols = pd.Index(['inv', 'bkl', 'accu'])
mask = df['upto_' cols '_threshold'].gt(50)
(
df[cols '_date']
.where(mask.to_numpy())
.groupby([df['stud_id'], df['prod_id']]).first()
.bfill(axis=1).iloc[:, 0]
.rename('fifty_pct_date')
.reset_index()
)
結果
stud_id prod_id fifty_pct_date
0 101 12 2011-10-22
uj5u.com熱心網友回復:
我認為您還可以通過以下代碼獲取目標日期:
代碼:
import pandas as pd
# Create a sample dataframe
df = pd.DataFrame({'stud_id': {0: 101, 1: 101, 2: 101}, 'prod_id': {0: 12, 1: 12, 2: 12}, 'total_qty': {0: 100, 1: 100, 2: 100}, 'ques_date': {0: '13/11/2010', 1: '13/11/2010', 2: '13/11/2010'}, 'inv_qty': {0: 7.0, 1: 7.0, 2: 7.0}, 'inv_date': {0: '16/02/2012', 1: '16/02/2012', 2: '16/02/2012'}, 'bkl_qty': {0: 15, 1: 40, 2: 2}, 'bkl_date': {0: '2013-01-16', 1: '2011-10-22', 2: '2019-09-10'}, 'csum': {0: 15, 1: 55, 2: 57}, 'accu_qty': {0: 10, 1: 10, 2: 10}, 'accu_date': {0: '13/08/2021', 1: '13/08/2021', 2: '13/08/2021'}, 'upto_inv_threshold': {0: 7.0, 1: 7.0, 2: 7.0}, 'upto_bkl_threshold': {0: 22.0, 1: 62.0, 2: 64.0}, 'upto_accu_threshold': {0: 32.0, 1: 72.0, 2: 74.0}})
# Transform df
symbols = ['inv', 'bkl', 'accu']
df1 = df.melt(['stud_id', 'prod_id'], [f'{s}_date' for s in symbols], value_name='date')
df2 = df.melt(['stud_id', 'prod_id'], [f'upto_{s}_threshold' for s in symbols], value_name='threshold')
# Merge and get the target date(s)
df = df1.join(df2.loc[df2.threshold>=50, 'threshold'], how='inner')
df = df.groupby(['stud_id', 'prod_id'], as_index=False)['date'].first()
print(df)
輸出:
| stud_id | prod_id | 日期 |
|---|---|---|
| 101 | 12 | 2011-10-22 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/433544.html
標籤:Python 熊猫 数据框 麻木的 熊猫-groupby
上一篇:對pddf進行子集化的最有效方法
