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












11















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



























    11















    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

























      11












      11








      11


      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 40 mins ago









      ChrisChris

      1,206214




      1,206214
























          1 Answer
          1






          active

          oldest

          votes


















          14














          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


          EDIT : If 1st and last element contains NaN then use (Dark
          suggestion):



          df = pd.DataFrame({'val':[np.nan,1,np.nan, 4, 5, np.nan, 
          10, 1,2,5, np.nan, np.nan, 9,np.nan,]})
          df = (df.ffill()+df.bfill())/2
          df = df.bfill().ffill()

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





          share|improve this answer





















          • 3





            That is just brilliant. Thanks a ton :)

            – Chris
            33 mins ago











          • @Chris Glad to help.

            – Sandeep Kadapa
            27 mins ago






          • 3





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

            – Dark
            12 mins ago











          • @anon01 Good point

            – Chris
            10 mins ago











          • @Dark Great suggestion :) Thanks for the insight

            – Chris
            9 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









          14














          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


          EDIT : If 1st and last element contains NaN then use (Dark
          suggestion):



          df = pd.DataFrame({'val':[np.nan,1,np.nan, 4, 5, np.nan, 
          10, 1,2,5, np.nan, np.nan, 9,np.nan,]})
          df = (df.ffill()+df.bfill())/2
          df = df.bfill().ffill()

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





          share|improve this answer





















          • 3





            That is just brilliant. Thanks a ton :)

            – Chris
            33 mins ago











          • @Chris Glad to help.

            – Sandeep Kadapa
            27 mins ago






          • 3





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

            – Dark
            12 mins ago











          • @anon01 Good point

            – Chris
            10 mins ago











          • @Dark Great suggestion :) Thanks for the insight

            – Chris
            9 mins ago
















          14














          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


          EDIT : If 1st and last element contains NaN then use (Dark
          suggestion):



          df = pd.DataFrame({'val':[np.nan,1,np.nan, 4, 5, np.nan, 
          10, 1,2,5, np.nan, np.nan, 9,np.nan,]})
          df = (df.ffill()+df.bfill())/2
          df = df.bfill().ffill()

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





          share|improve this answer





















          • 3





            That is just brilliant. Thanks a ton :)

            – Chris
            33 mins ago











          • @Chris Glad to help.

            – Sandeep Kadapa
            27 mins ago






          • 3





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

            – Dark
            12 mins ago











          • @anon01 Good point

            – Chris
            10 mins ago











          • @Dark Great suggestion :) Thanks for the insight

            – Chris
            9 mins ago














          14












          14








          14







          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


          EDIT : If 1st and last element contains NaN then use (Dark
          suggestion):



          df = pd.DataFrame({'val':[np.nan,1,np.nan, 4, 5, np.nan, 
          10, 1,2,5, np.nan, np.nan, 9,np.nan,]})
          df = (df.ffill()+df.bfill())/2
          df = df.bfill().ffill()

          print(df)
          val
          0 1.0
          1 1.0
          2 2.5
          3 4.0
          4 5.0
          5 7.5
          6 10.0
          7 1.0
          8 2.0
          9 5.0
          10 7.0
          11 7.0
          12 9.0
          13 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


          EDIT : If 1st and last element contains NaN then use (Dark
          suggestion):



          df = pd.DataFrame({'val':[np.nan,1,np.nan, 4, 5, np.nan, 
          10, 1,2,5, np.nan, np.nan, 9,np.nan,]})
          df = (df.ffill()+df.bfill())/2
          df = df.bfill().ffill()

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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 8 mins ago

























          answered 34 mins ago









          Sandeep KadapaSandeep Kadapa

          6,873630




          6,873630








          • 3





            That is just brilliant. Thanks a ton :)

            – Chris
            33 mins ago











          • @Chris Glad to help.

            – Sandeep Kadapa
            27 mins ago






          • 3





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

            – Dark
            12 mins ago











          • @anon01 Good point

            – Chris
            10 mins ago











          • @Dark Great suggestion :) Thanks for the insight

            – Chris
            9 mins ago














          • 3





            That is just brilliant. Thanks a ton :)

            – Chris
            33 mins ago











          • @Chris Glad to help.

            – Sandeep Kadapa
            27 mins ago






          • 3





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

            – Dark
            12 mins ago











          • @anon01 Good point

            – Chris
            10 mins ago











          • @Dark Great suggestion :) Thanks for the insight

            – Chris
            9 mins ago








          3




          3





          That is just brilliant. Thanks a ton :)

          – Chris
          33 mins ago





          That is just brilliant. Thanks a ton :)

          – Chris
          33 mins ago













          @Chris Glad to help.

          – Sandeep Kadapa
          27 mins ago





          @Chris Glad to help.

          – Sandeep Kadapa
          27 mins ago




          3




          3





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

          – Dark
          12 mins ago





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

          – Dark
          12 mins ago













          @anon01 Good point

          – Chris
          10 mins ago





          @anon01 Good point

          – Chris
          10 mins ago













          @Dark Great suggestion :) Thanks for the insight

          – Chris
          9 mins ago





          @Dark Great suggestion :) Thanks for the insight

          – Chris
          9 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

          Knooppunt Holsloot

          Altaar (religie)

          Gregoriusmis