我有一個資料框,如果滿足某個條件,我想基本上創建該行的副本。如果 'Date' = Q4.22 或 > AND type = 'live' 則應復制行 此外,對于創建的每個副本,應更新“unit”計數以反映這一點(按 id 和 Date 分組) 一旦建立副本,單位計數應反映基于相同 id 和 Date 的新計數。
資料
id Date set type unit energy
bb Q4.22 l live l01 20
bb Q4.22 l live l02 20
ba Q3.22 l non l01 20
aa Q4.22 l non l01 20
aa Q4.22 l live l01 20
cc Q3.22 l non l01 20
aa Q4.22 l live l02 20
期望的
id Date set type unit energy
bb Q4.22 l live l01 20
bb Q4.22 l live l02 20
bb Q4.22 l live l03 20
bb Q4.22 l live l04 20
ba Q3.22 l non l01 20
aa Q4.22 l non l01 20
aa Q4.22 l live l01 20
aa Q4.22 l live l02 20
cc Q3.22 l non l01 20
aa Q4.22 l live l03 20
aa Q4.22 l live l04 20
正在做
pd.concat([df, df.loc[(df['Date'] == > 'Q4.22') & (df['live'] == 'live')]])
但是,我仍然需要為創建的新副本添加計數器。任何建議表示贊賞。
uj5u.com熱心網友回復:
嘗試:
- 將您的日期列轉換為時間戳
concat您的原始資料和過濾后的資料groupby獲取cumcount“id”和“日期”并相應地設定“單位”
df["Date"] = pd.to_datetime(df["Date"].str.replace(r"(Q\d).(\d )", r"\2-\1",regex=True))
output = pd.concat([df, df[df["Date"].ge(pd.Timestamp("2022-10-01"))&df["type"].eq("live")]], ignore_index=True)
output["unit"] = output["set"] output.groupby(["id", "Date"]).cumcount().add(1).astype(str).str.zfill(2)
output = output.sort_values("id", ignore_index=True)
#convert Date back to original format if needed
output["Date"] = output["Date"].dt.to_period("Q").astype(str).str.replace(r"\d\d(\d )(Q\d)",r"\2.\1",regex=True)
>>> output
id Date set type unit energy
0 aa Q4.22 l non l01 20
1 aa Q4.22 l live l02 20
2 aa Q4.22 l live l03 20
3 aa Q4.22 l live l04 20
4 aa Q4.22 l live l05 20
5 ba Q3.22 l non l01 20
6 bb Q4.22 l live l01 20
7 bb Q4.22 l live l02 20
8 bb Q4.22 l live l03 20
9 bb Q4.22 l live l04 20
10 cc Q3.22 l non l01 20
uj5u.com熱心網友回復:
首先,如注釋中所述,我們需要將一些 df 列轉換為更方便的型別:
int對于unit(剝離任何字符),pd.Period對于Date.
df2 = df.assign(
unit=df['unit'].str.extract(r'(\d )').astype(int),
period=df['Date'].str.replace(r'^(Q\d)\D*(\d )$', r'\2\1', regex=True).apply(pd.Period)
)
>>> df2
id Date set type unit energy period
0 bb Q4.22 l live 1 20 2022Q4
1 bb Q4.22 l live 2 20 2022Q4
2 ba Q3.22 l non 1 20 2022Q3
3 aa Q4.22 l non 1 20 2022Q4
4 aa Q4.22 l live 1 20 2022Q4
5 cc Q3.22 l non 1 20 2022Q3
6 aa Q4.22 l live 2 20 2022Q4
>>> df2.dtypes
id object
Date object
set object
type object
unit int64
energy int64
period period[Q-DEC]
dtype: object
完成后,現在我們可以繼續問題本身的邏輯了。
ix_repeat = (df2['period'] >= pd.Period('2022-Q4')) & (df2['type'] == 'live')
r = df2.loc[ix_repeat]
r.assign(unit=r['unit'] r.groupby(['id', 'period'])['unit'].transform(max))
>>> r
id Date set type unit energy period
0 bb Q4.22 l live 3 20 2022Q4
1 bb Q4.22 l live 4 20 2022Q4
4 aa Q4.22 l live 3 20 2022Q4
6 aa Q4.22 l live 4 20 2022Q4
# finally
df2 = pd.concat([df2, r])
可選:將單元帶回其奇怪的字串版本:
df2 = df2.assign(unit=df2['set'] df2['unit'].astype(str).str.zfill(2))
>>> df2
id Date set type unit energy period
0 bb Q4.22 l live l01 20 2022Q4
1 bb Q4.22 l live l02 20 2022Q4
2 ba Q3.22 l non l01 20 2022Q3
3 aa Q4.22 l non l01 20 2022Q4
4 aa Q4.22 l live l01 20 2022Q4
5 cc Q3.22 l non l01 20 2022Q3
6 aa Q4.22 l live l02 20 2022Q4
0 bb Q4.22 l live l03 20 2022Q4
1 bb Q4.22 l live l04 20 2022Q4
4 aa Q4.22 l live l03 20 2022Q4
6 aa Q4.22 l live l04 20 2022Q4
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/473667.html
