简体   繁体   中英

Double dataframe values when certain condition is met using Python

I have a dataframe where if a certain condition is met, I'd like to essentially create a duplicate of that row. Row should be duplicated IF 'Date' = Q4.22 or > AND type = 'live'

Data

id  Date    set     type    unit    energy
bb  Q4.22   L       live    l01     20
ba  Q4.22   L       non     l01     20
ba  Q3.22   L       non     l01     20
aa  Q4.22   L       non     l01     20
bb  Q4.22   L       live    l01     20
cc  Q3.22   L       non     l01     20
ca  Q3.22   L       live    l01     20

Desired

id  Date    set     type    unit    energy
bb  Q4.22   L       live    l01     20
bb  Q4.22   L       live    l01     20
ba  Q4.22   L       non     l01     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    l01     20
cc  Q3.22   L       non     l01     20
ca  Q3.22   L       live    l01     20

Doing

new = np.arange(len(dupe)).repeat((~dupe.duplicated(keep=False).values) + 1)

I'm thinking this is a start, but unsure how to add the conditions. Any suggestion is appreciated.

使用所需条件创建数据框的过滤视图并与原始数据框连接:

pd.concat([df, df.loc[(df['Date'] == 'Q4.22') & (df['live'] == 'live')]])

you can try concat

pd.concat([df, df[(df['Date']>= "Q4.22") & (df['type'] == 'live')]]).reset_index()
    index   id  Date    set     type    unit    energy
0   0   bb  Q4.22   L   live    l01     20
1   1   ba  Q4.22   L   non     l01     20
2   2   ba  Q3.22   L   non     l01     20
3   3   aa  Q4.22   L   non     l01     20
4   4   aa  Q4.22   L   live    l01     20
5   5   cc  Q3.22   L   non     l01     20
6   6   ca  Q3.22   L   live    l01     20
7   0   bb  Q4.22   L   live    l01     20
8   4   aa  Q4.22   L   live    l01     20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM