Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy Previous Row Values when Current Value is 0 (Zero)

My Dataframe as below:

13664567    74.3    SELL    1581566.4   2378211.6   12:07:59
13665406    74.3    0       0           0           0
13665406    0       0       0           0           0
13665406    74.3    0       0           0           0
13667531    74.3    0       0           0           0
13667531    74.3    0       0           0           0
13672281    74.3    0       0           0           0
13672591    74.3    0       0           0           0
13672591    74.3    BUY     2388742.8   1590276     12:08:01
13681398    74.3    0       0           0           0
13681398    74.3    0       0           0           0
13684573    74.3    0       0           0           0
13685574    74.3    0       0           0           0
13685574    74.3    0       0           0           0
13688544    0       0       0           0           0
13689596    74.3    0       0           0           0
13689596    74.3    0       0           0           0
13705735    74.3    0       0           0           0
13706035    74.3    0       0           0           0
13712130    74.3    SELL    1583219.2   2408620.8   12:08:01
13712130    74.3    0       0           0           0
13715699    74.3    0       0           0           0
13720809    74.3    0       0           0           0
13726310    74.3    0       0           0           0
13726310    74.3    0       0           0           0
13726410    74.3    0       0           0           0

I want to fill the 0 (Zero) values of last 4 Columns with previous row values.

I want dataframe like as below:

13664567    74.3    SELL    1581566.4    2378212    12:07:59
13665406    74.3    SELL    1581566.4    2378212    12:07:59
13665406    0       SELL    1581566.4    2378212    12:07:59
13665406    74.3    SELL    1581566.4    2378212    12:07:59
13667531    74.3    SELL    1581566.4    2378212    12:07:59
13667531    74.3    SELL    1581566.4    2378212    12:07:59
13672281    74.3    SELL    1581566.4    2378212    12:07:59
13672591    74.3    SELL    1581566.4    2378212    12:07:59
13672591    74.3    BUY     2388742.8    1590276    12:08:01
13681398    74.3    BUY     2388742.8    1590276    12:08:01
13681398    74.3    BUY     2388742.8    1590276    12:08:01
13684573    74.3    BUY     2388742.8    1590276    12:08:01
13685574    74.3    BUY     2388742.8    1590276    12:08:01
13685574    74.3    BUY     2388742.8    1590276    12:08:01
13688544    0       BUY     2388742.8    1590276    12:08:01
13689596    74.3    BUY     2388742.8    1590276    12:08:01
13689596    74.3    BUY     2388742.8    1590276    12:08:01
13705735    74.3    BUY     2388742.8    1590276    12:08:01
13706035    74.3    BUY     2388742.8    1590276    12:08:01
13712130    74.3    SELL    1583219.2    2408621    12:08:01
13712130    74.3    SELL    1583219.2    2408621    12:08:01
13715699    74.3    SELL    1583219.2    2408621    12:08:01
13720809    74.3    SELL    1583219.2    2408621    12:08:01
13726310    74.3    SELL    1583219.2    2408621    12:08:01
13726310    74.3    SELL    1583219.2    2408621    12:08:01
13726410    74.3    SELL    1583219.2    2408621    12:08:01

I want to make the changes only to last four columns and copy the previous row values to next row until the New Value is available.

Please help.

like image 909
Pravat Avatar asked May 19 '26 21:05

Pravat


2 Answers

Use update with select last 4 columns by iloc, replace by missing values and forward fill last previous values:

df.update(df.iloc[:, -4:].replace([0,'0'], np.nan).ffill())

Or use mask for replace 0:

df.update(df.iloc[:, -4:].mask(lambda x: x.isin([0, '0'])).ffill())
#if no string 0
#df.update(df.iloc[:, -4:].mask(lambda x: x == 0)).ffill())

print (df)
           a     b     c          d          e         f
0   13664567  74.3  SELL  1581566.4  2378211.6  12:07:59
1   13665406  74.3  SELL  1581566.4  2378211.6  12:07:59
2   13665406  74.3  SELL  1581566.4  2378211.6  12:07:59
3   13665406  74.3  SELL  1581566.4  2378211.6  12:07:59
4   13667531  74.3  SELL  1581566.4  2378211.6  12:07:59
5   13667531  74.3  SELL  1581566.4  2378211.6  12:07:59
6   13672281  74.3  SELL  1581566.4  2378211.6  12:07:59
7   13672591  74.3  SELL  1581566.4  2378211.6  12:07:59
8   13672591  74.3   BUY  2388742.8  1590276.0  12:08:01
9   13681398  74.3   BUY  2388742.8  1590276.0  12:08:01
10  13681398  74.3   BUY  2388742.8  1590276.0  12:08:01
11  13684573  74.3   BUY  2388742.8  1590276.0  12:08:01
12  13685574  74.3   BUY  2388742.8  1590276.0  12:08:01
13  13685574  74.3   BUY  2388742.8  1590276.0  12:08:01
14  13688544  74.3   BUY  2388742.8  1590276.0  12:08:01
15  13689596  74.3   BUY  2388742.8  1590276.0  12:08:01
16  13689596  74.3   BUY  2388742.8  1590276.0  12:08:01
17  13705735  74.3   BUY  2388742.8  1590276.0  12:08:01
18  13706035  74.3   BUY  2388742.8  1590276.0  12:08:01
19  13712130  74.3  SELL  1583219.2  2408620.8  12:08:01
20  13712130  74.3  SELL  1583219.2  2408620.8  12:08:01
21  13715699  74.3  SELL  1583219.2  2408620.8  12:08:01
22  13720809  74.3  SELL  1583219.2  2408620.8  12:08:01
23  13726310  74.3  SELL  1583219.2  2408620.8  12:08:01
24  13726310  74.3  SELL  1583219.2  2408620.8  12:08:01
25  13726410  74.3  SELL  1583219.2  2408620.8  12:08:01
like image 159
jezrael Avatar answered May 21 '26 14:05

jezrael


You can use mask + ffill:

values = df.iloc[:, -4:]
df.iloc[:, -4:] = values.mask(values.isin([0, '0'])).ffill()
like image 22
jpp Avatar answered May 21 '26 14:05

jpp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!