I have the following dataframe called df,
JavaScript
x
15
15
1
date flag1 flag2 flag3 flag4…
2
2020-12-31
3
2021-01-01
4
2021-01-02 1
5
2021-01-03
6
2021-01-04
7
2021-01-05 1
8
2021-01-06 1
9
2021-01-07
10
2021-01-08
11
2021-01-09
12
2021-01-10
13
2021-01-11 1 1
14
2021-01-12
15
I want to do a backfill when a 1 appears in any column, and fill backwards until a number appears or failing that, backfill til a set number.
So let’s say the set number to reduce o to is 0 and the decrement is 0.1, it should look like this,
JavaScript
1
15
15
1
date flag1 flag2 flag3 flag4…
2
2020-12-31 0.5 0.8 0.4
3
2021-01-01 0.0 0.6 0.9 0.5
4
2021-01-02 0.1 0.7 1.0 0.6
5
2021-01-03 0.2 0.8 0.7
6
2021-01-04 0.3 0.9 0.8
7
2021-01-05 0.4 1.0 0.9
8
2021-01-06 0.5 1.0
9
2021-01-07 0.6 0.6
10
2021-01-08 0.7 0.7
11
2021-01-09 0.8 0.8
12
2021-01-10 0.9 0.9
13
2021-01-11 1.0 1.0
14
2021-01-12
15
Can this be achieved with pandas? I want to be able to set the decrement amount and the limit for example the above would be 0.1 and 0.
I know that this command can increment the values backwards:
JavaScript
1
3
1
df1 = df1[::-1].fillna(method='ffill')
2
(df1 + (df1 == df1.shift()).cumsum()).sort_index()
3
But that’s not what I want.
Advertisement
Answer
You could also try using iloc
to change the values based on the indices where the column value is equals to 1.0:
JavaScript
1
10
10
1
import pandas as pd
2
import numpy as np
3
4
def process_data(c, n):
5
for idx in reversed(np.where(c==1)[0]):
6
c.iloc[np.arange(idx)[::-1][:n.shape[0]]] = n[idx-1::-1][::-1]
7
c.iat[idx] = 1.0
8
return c
9
df = df.apply(lambda r: process_data(r, np.linspace(1.0, 0.0, num=11)[1:]))
10
JavaScript
1
16
16
1
flag1 flag2 flag3 flag4
2
date
3
2020-12-31 NaN 0.5 0.8 0.4
4
2021-01-01 0.0 0.6 0.9 0.5
5
2021-01-02 0.1 0.7 1.0 0.6
6
2021-01-03 0.2 0.8 NaN 0.7
7
2021-01-04 0.3 0.9 NaN 0.8
8
2021-01-05 0.4 1.0 NaN 0.9
9
2021-01-06 0.5 NaN NaN 1.0
10
2021-01-07 0.6 NaN NaN 0.6
11
2021-01-08 0.7 NaN NaN 0.7
12
2021-01-09 0.8 NaN NaN 0.8
13
2021-01-10 0.9 NaN NaN 0.9
14
2021-01-11 1.0 NaN NaN 1.0
15
2021-01-12 NaN NaN NaN NaN
16