blueice20
blueice20

Reputation: 19

Python copying value from row before if condition is met

I do have the following dataframe:

             A    B
Date
2019-11-04   0    0
2019-11-05   0    0
2019-11-06   1   -2.197387
2019-11-07   1   -2.203836
2019-11-08   1   -2.208839
2019-11-09  -1   -2.207335
2022-11-10  -1   -0.968055
2022-11-11  -1   -0.971627
2022-11-12   0    0
2022-11-13   1   -0.974535
2022-11-14   1   -0.972994

What I'm expecting is the following:

             A    B
Date
2019-11-04   0    0
2019-11-05   0    0
2019-11-06   1   -2.197387
2019-11-07   1   -2.197387
2019-11-08   1   -2.197387
2019-11-09  -1   -2.207335
2022-11-10  -1   -2.207335
2022-11-11  -1   -2.207335
2022-11-12   0    0
2022-11-13   1   -0.974535
2022-11-14   1   -0.974535

I tried .loc[-1] and .iloc[-1] with the condition that .iloc or .iloc has to be !=0 but in both cases the 0 was just copied over to the next row.

Upvotes: 1

Views: 112

Answers (1)

jezrael
jezrael

Reputation: 862501

Use GroupBy.pipe and GroupBy.first with consecutive values of A created by compared by shifted values with Series.cumsum:

df['B'] = df.groupby(df.A.ne(df.A.shift()).cumsum())['B'].transform('first')
print (df)
            A         B
Date                   
2019-11-04  0  0.000000
2019-11-05  0  0.000000
2019-11-06  1 -2.197387
2019-11-07  1 -2.197387
2019-11-08  1 -2.197387
2019-11-09 -1 -2.207335
2022-11-10 -1 -2.207335
2022-11-11 -1 -2.207335
2022-11-12  0  0.000000
2022-11-13  1 -0.974535
2022-11-14  1 -0.974535

Another idea is use Series.where for replace not first values per consecutive values and forward filling missing values (this working if no missing values in column):

df['B'] = df['B'].where(df.A.ne(df.A.shift())).ffill()
print (df)
            A         B
Date                   
2019-11-04  0  0.000000
2019-11-05  0  0.000000
2019-11-06  1 -2.197387
2019-11-07  1 -2.197387
2019-11-08  1 -2.197387
2019-11-09 -1 -2.207335
2022-11-10 -1 -2.207335
2022-11-11 -1 -2.207335
2022-11-12  0  0.000000
2022-11-13  1 -0.974535
2022-11-14  1 -0.974535

Upvotes: 1

Related Questions