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











up vote
2
down vote

favorite












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.










share|improve this question




























    up vote
    2
    down vote

    favorite












    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.










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      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.










      share|improve this question















      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.







      python python-3.x dataframe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 4 at 10:21

























      asked Nov 4 at 10:11









      Pravat

      698




      698
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          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





          share|improve this answer



















          • 1




            Sir, Is it possible to select particular columns as I want to apply to last 4 columns only.
            – Pravat
            Nov 4 at 10:16






          • 1




            @Pravat - Super, glad can help! :)
            – jezrael
            Nov 4 at 10:32


















          up vote
          1
          down vote













          You can use mask + ffill:



          values = df.iloc[:, -4:]
          df.iloc[:, -4:] = values.mask(values.isin([0, '0'])).ffill()





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139680%2fcopy-previous-row-values-when-current-value-is-0-zero%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            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





            share|improve this answer



















            • 1




              Sir, Is it possible to select particular columns as I want to apply to last 4 columns only.
              – Pravat
              Nov 4 at 10:16






            • 1




              @Pravat - Super, glad can help! :)
              – jezrael
              Nov 4 at 10:32















            up vote
            3
            down vote



            accepted










            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





            share|improve this answer



















            • 1




              Sir, Is it possible to select particular columns as I want to apply to last 4 columns only.
              – Pravat
              Nov 4 at 10:16






            • 1




              @Pravat - Super, glad can help! :)
              – jezrael
              Nov 4 at 10:32













            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            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





            share|improve this answer














            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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 4 at 10:26

























            answered Nov 4 at 10:12









            jezrael

            303k20233309




            303k20233309








            • 1




              Sir, Is it possible to select particular columns as I want to apply to last 4 columns only.
              – Pravat
              Nov 4 at 10:16






            • 1




              @Pravat - Super, glad can help! :)
              – jezrael
              Nov 4 at 10:32














            • 1




              Sir, Is it possible to select particular columns as I want to apply to last 4 columns only.
              – Pravat
              Nov 4 at 10:16






            • 1




              @Pravat - Super, glad can help! :)
              – jezrael
              Nov 4 at 10:32








            1




            1




            Sir, Is it possible to select particular columns as I want to apply to last 4 columns only.
            – Pravat
            Nov 4 at 10:16




            Sir, Is it possible to select particular columns as I want to apply to last 4 columns only.
            – Pravat
            Nov 4 at 10:16




            1




            1




            @Pravat - Super, glad can help! :)
            – jezrael
            Nov 4 at 10:32




            @Pravat - Super, glad can help! :)
            – jezrael
            Nov 4 at 10:32












            up vote
            1
            down vote













            You can use mask + ffill:



            values = df.iloc[:, -4:]
            df.iloc[:, -4:] = values.mask(values.isin([0, '0'])).ffill()





            share|improve this answer

























              up vote
              1
              down vote













              You can use mask + ffill:



              values = df.iloc[:, -4:]
              df.iloc[:, -4:] = values.mask(values.isin([0, '0'])).ffill()





              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                You can use mask + ffill:



                values = df.iloc[:, -4:]
                df.iloc[:, -4:] = values.mask(values.isin([0, '0'])).ffill()





                share|improve this answer












                You can use mask + ffill:



                values = df.iloc[:, -4:]
                df.iloc[:, -4:] = values.mask(values.isin([0, '0'])).ffill()






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 4 at 10:28









                jpp

                78.8k184592




                78.8k184592






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139680%2fcopy-previous-row-values-when-current-value-is-0-zero%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()