Home > Mobile >  Double dataframe values when certain condition is met using Python
Double dataframe values when certain condition is met using Python

Time:05-12

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.

CodePudding user response:

Create a filtered view of the dataframe using the desired criteria and concatenate with the original dataframe:

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

CodePudding user response:

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
  • Related