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

Pravat

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.

jezrael

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Compare current row value to previous row values

Validate the current row of a column, if zero give the previous row's value or else provide the current row's value

how to set a value at a current level to zero based on the previous values

Compare current row value to all 6 previous row values

Count values in previous rows that are greater than current row value

Calculating a value in SQL using previous row's values and current row value

Mysql substract value of previous row to current row

Subtract previous row value to current row

Adding a previous row value with current row

Selecting rows where column value is 1 in the current row, but 0 in the previous row

dropping current row if the value of current row is the same as previous row in R

identify zero values from each customer id then get the value from previous row next column pandas

fill each row of a dataframe with zero starting from the column in which the values decreases with respect to the value in the previous column

oracle calculate the average of the current and previous row values

python comparing values of previous rows with current row

sqlite count 1 value based on all previous row and restart count when row value is 0

Get previous value of current row in select

Compare Value of Current and Previous Row in Spark

How to copy the previous row value of the same column?

Check if values in all n previous rows are greater than the value of current row

Copy format of previous row when Gforms is submitted

how to make pandas row value to zero when row above values are zeros and below value not equal to zero using python pandas

how to replace the zero values with the previous non-zero value in MySQL?

Subtract previous row value from the current row value in a Pandas column

How to multiply value in previous row with a value from current row

Dynamically add previous row value to current row value in SQL(Postgres)

In PostgreSQL, how to select the previous row value to compute the current row value?

add current row value with previous row calculated value

How to find the value current row value if current row value match with previous row value in power bi?