pandas filling nans by mean of before and after non-nan values












8















I would like to fill df's nan with an average of adjacent elements.



Consider a dataframe:



df = pd.DataFrame({'val': [1,np.nan, 4, 5, np.nan, 10, 1,2,5, np.nan, np.nan, 9]})
val
0 1.0
1 NaN
2 4.0
3 5.0
4 NaN
5 10.0
6 1.0
7 2.0
8 5.0
9 NaN
10 NaN
11 9.0


My desired output is:



    val
0 1.0
1 2.5
2 4.0
3 5.0
4 7.5
5 10.0
6 1.0
7 2.0
8 5.0
9 7.0 <<< deadend
10 7.0 <<< deadend
11 9.0


I've looked into other solutions such as Fill cell containing NaN with average of value before and after, but this won't work in case of two or more consecutive np.nans.



Any help is greatly appreciated!










share|improve this question



























    8















    I would like to fill df's nan with an average of adjacent elements.



    Consider a dataframe:



    df = pd.DataFrame({'val': [1,np.nan, 4, 5, np.nan, 10, 1,2,5, np.nan, np.nan, 9]})
    val
    0 1.0
    1 NaN
    2 4.0
    3 5.0
    4 NaN
    5 10.0
    6 1.0
    7 2.0
    8 5.0
    9 NaN
    10 NaN
    11 9.0


    My desired output is:



        val
    0 1.0
    1 2.5
    2 4.0
    3 5.0
    4 7.5
    5 10.0
    6 1.0
    7 2.0
    8 5.0
    9 7.0 <<< deadend
    10 7.0 <<< deadend
    11 9.0


    I've looked into other solutions such as Fill cell containing NaN with average of value before and after, but this won't work in case of two or more consecutive np.nans.



    Any help is greatly appreciated!










    share|improve this question

























      8












      8








      8


      1






      I would like to fill df's nan with an average of adjacent elements.



      Consider a dataframe:



      df = pd.DataFrame({'val': [1,np.nan, 4, 5, np.nan, 10, 1,2,5, np.nan, np.nan, 9]})
      val
      0 1.0
      1 NaN
      2 4.0
      3 5.0
      4 NaN
      5 10.0
      6 1.0
      7 2.0
      8 5.0
      9 NaN
      10 NaN
      11 9.0


      My desired output is:



          val
      0 1.0
      1 2.5
      2 4.0
      3 5.0
      4 7.5
      5 10.0
      6 1.0
      7 2.0
      8 5.0
      9 7.0 <<< deadend
      10 7.0 <<< deadend
      11 9.0


      I've looked into other solutions such as Fill cell containing NaN with average of value before and after, but this won't work in case of two or more consecutive np.nans.



      Any help is greatly appreciated!










      share|improve this question














      I would like to fill df's nan with an average of adjacent elements.



      Consider a dataframe:



      df = pd.DataFrame({'val': [1,np.nan, 4, 5, np.nan, 10, 1,2,5, np.nan, np.nan, 9]})
      val
      0 1.0
      1 NaN
      2 4.0
      3 5.0
      4 NaN
      5 10.0
      6 1.0
      7 2.0
      8 5.0
      9 NaN
      10 NaN
      11 9.0


      My desired output is:



          val
      0 1.0
      1 2.5
      2 4.0
      3 5.0
      4 7.5
      5 10.0
      6 1.0
      7 2.0
      8 5.0
      9 7.0 <<< deadend
      10 7.0 <<< deadend
      11 9.0


      I've looked into other solutions such as Fill cell containing NaN with average of value before and after, but this won't work in case of two or more consecutive np.nans.



      Any help is greatly appreciated!







      python pandas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 29 mins ago









      ChrisChris

      1,191213




      1,191213
























          1 Answer
          1






          active

          oldest

          votes


















          11














          Use ffill + bfill and divide by 2:



          df = (df.ffill()+df.bfill())/2

          print(df)
          val
          0 1.0
          1 2.5
          2 4.0
          3 5.0
          4 7.5
          5 10.0
          6 1.0
          7 2.0
          8 5.0
          9 7.0
          10 7.0
          11 9.0





          share|improve this answer





















          • 3





            That is just brilliant. Thanks a ton :)

            – Chris
            22 mins ago











          • @Chris Glad to help.

            – Sandeep Kadapa
            17 mins ago











          • this will fail if the first or last element is a nan

            – anon01
            5 mins ago






          • 2





            If first and last elements are nan. Then use df.bfill().ffill() after using the above solution.

            – Dark
            2 mins ago











          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',
          autoActivateHeartbeat: false,
          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%2f54414269%2fpandas-filling-nans-by-mean-of-before-and-after-non-nan-values%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          11














          Use ffill + bfill and divide by 2:



          df = (df.ffill()+df.bfill())/2

          print(df)
          val
          0 1.0
          1 2.5
          2 4.0
          3 5.0
          4 7.5
          5 10.0
          6 1.0
          7 2.0
          8 5.0
          9 7.0
          10 7.0
          11 9.0





          share|improve this answer





















          • 3





            That is just brilliant. Thanks a ton :)

            – Chris
            22 mins ago











          • @Chris Glad to help.

            – Sandeep Kadapa
            17 mins ago











          • this will fail if the first or last element is a nan

            – anon01
            5 mins ago






          • 2





            If first and last elements are nan. Then use df.bfill().ffill() after using the above solution.

            – Dark
            2 mins ago
















          11














          Use ffill + bfill and divide by 2:



          df = (df.ffill()+df.bfill())/2

          print(df)
          val
          0 1.0
          1 2.5
          2 4.0
          3 5.0
          4 7.5
          5 10.0
          6 1.0
          7 2.0
          8 5.0
          9 7.0
          10 7.0
          11 9.0





          share|improve this answer





















          • 3





            That is just brilliant. Thanks a ton :)

            – Chris
            22 mins ago











          • @Chris Glad to help.

            – Sandeep Kadapa
            17 mins ago











          • this will fail if the first or last element is a nan

            – anon01
            5 mins ago






          • 2





            If first and last elements are nan. Then use df.bfill().ffill() after using the above solution.

            – Dark
            2 mins ago














          11












          11








          11







          Use ffill + bfill and divide by 2:



          df = (df.ffill()+df.bfill())/2

          print(df)
          val
          0 1.0
          1 2.5
          2 4.0
          3 5.0
          4 7.5
          5 10.0
          6 1.0
          7 2.0
          8 5.0
          9 7.0
          10 7.0
          11 9.0





          share|improve this answer















          Use ffill + bfill and divide by 2:



          df = (df.ffill()+df.bfill())/2

          print(df)
          val
          0 1.0
          1 2.5
          2 4.0
          3 5.0
          4 7.5
          5 10.0
          6 1.0
          7 2.0
          8 5.0
          9 7.0
          10 7.0
          11 9.0






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 10 mins ago

























          answered 24 mins ago









          Sandeep KadapaSandeep Kadapa

          6,843630




          6,843630








          • 3





            That is just brilliant. Thanks a ton :)

            – Chris
            22 mins ago











          • @Chris Glad to help.

            – Sandeep Kadapa
            17 mins ago











          • this will fail if the first or last element is a nan

            – anon01
            5 mins ago






          • 2





            If first and last elements are nan. Then use df.bfill().ffill() after using the above solution.

            – Dark
            2 mins ago














          • 3





            That is just brilliant. Thanks a ton :)

            – Chris
            22 mins ago











          • @Chris Glad to help.

            – Sandeep Kadapa
            17 mins ago











          • this will fail if the first or last element is a nan

            – anon01
            5 mins ago






          • 2





            If first and last elements are nan. Then use df.bfill().ffill() after using the above solution.

            – Dark
            2 mins ago








          3




          3





          That is just brilliant. Thanks a ton :)

          – Chris
          22 mins ago





          That is just brilliant. Thanks a ton :)

          – Chris
          22 mins ago













          @Chris Glad to help.

          – Sandeep Kadapa
          17 mins ago





          @Chris Glad to help.

          – Sandeep Kadapa
          17 mins ago













          this will fail if the first or last element is a nan

          – anon01
          5 mins ago





          this will fail if the first or last element is a nan

          – anon01
          5 mins ago




          2




          2





          If first and last elements are nan. Then use df.bfill().ffill() after using the above solution.

          – Dark
          2 mins ago





          If first and last elements are nan. Then use df.bfill().ffill() after using the above solution.

          – Dark
          2 mins ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54414269%2fpandas-filling-nans-by-mean-of-before-and-after-non-nan-values%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Сан-Квентин

          8-я гвардейская общевойсковая армия

          Алькесар