Pandas Summing Two Columns with Nan












2















I have three columns in pandas dataframes with Nan:



>>> d=pd.DataFrame({'col1': [1, 2], 'col2': [3, 4], 'col3':[5,6]})
>>> d
col1 col2 col3
0 1 3 5
1 2 4 6
>>> d['col2'].iloc[0]=np.nan
>>> d
col1 col2 col3
0 1 NaN 5
1 2 4.0 6
>>> d['col1'].iloc[1]=np.nan
>>> d
col1 col2 col3
0 1.0 NaN 5
1 NaN 4.0 6
>>> d['col3'].iloc[1]=np.nan
>>> d
col1 col2 col3
0 1.0 NaN 5.0
1 NaN 4.0 NaN


Now, I would like the column addition to have the following output:



>>> d['col1']+d['col3']
0 6.0
1 NaN

>>> d['col1']+d['col2']
0 1.0
1 4.0


However, in reality, the output is instead:



>>> d['col1']+d['col3']
0 6.0
1 NaN

>>> d['col1']+d['col2']
0 NaN
1 NaN


Anyone knows how to achieve this?










share|improve this question



























    2















    I have three columns in pandas dataframes with Nan:



    >>> d=pd.DataFrame({'col1': [1, 2], 'col2': [3, 4], 'col3':[5,6]})
    >>> d
    col1 col2 col3
    0 1 3 5
    1 2 4 6
    >>> d['col2'].iloc[0]=np.nan
    >>> d
    col1 col2 col3
    0 1 NaN 5
    1 2 4.0 6
    >>> d['col1'].iloc[1]=np.nan
    >>> d
    col1 col2 col3
    0 1.0 NaN 5
    1 NaN 4.0 6
    >>> d['col3'].iloc[1]=np.nan
    >>> d
    col1 col2 col3
    0 1.0 NaN 5.0
    1 NaN 4.0 NaN


    Now, I would like the column addition to have the following output:



    >>> d['col1']+d['col3']
    0 6.0
    1 NaN

    >>> d['col1']+d['col2']
    0 1.0
    1 4.0


    However, in reality, the output is instead:



    >>> d['col1']+d['col3']
    0 6.0
    1 NaN

    >>> d['col1']+d['col2']
    0 NaN
    1 NaN


    Anyone knows how to achieve this?










    share|improve this question

























      2












      2








      2








      I have three columns in pandas dataframes with Nan:



      >>> d=pd.DataFrame({'col1': [1, 2], 'col2': [3, 4], 'col3':[5,6]})
      >>> d
      col1 col2 col3
      0 1 3 5
      1 2 4 6
      >>> d['col2'].iloc[0]=np.nan
      >>> d
      col1 col2 col3
      0 1 NaN 5
      1 2 4.0 6
      >>> d['col1'].iloc[1]=np.nan
      >>> d
      col1 col2 col3
      0 1.0 NaN 5
      1 NaN 4.0 6
      >>> d['col3'].iloc[1]=np.nan
      >>> d
      col1 col2 col3
      0 1.0 NaN 5.0
      1 NaN 4.0 NaN


      Now, I would like the column addition to have the following output:



      >>> d['col1']+d['col3']
      0 6.0
      1 NaN

      >>> d['col1']+d['col2']
      0 1.0
      1 4.0


      However, in reality, the output is instead:



      >>> d['col1']+d['col3']
      0 6.0
      1 NaN

      >>> d['col1']+d['col2']
      0 NaN
      1 NaN


      Anyone knows how to achieve this?










      share|improve this question














      I have three columns in pandas dataframes with Nan:



      >>> d=pd.DataFrame({'col1': [1, 2], 'col2': [3, 4], 'col3':[5,6]})
      >>> d
      col1 col2 col3
      0 1 3 5
      1 2 4 6
      >>> d['col2'].iloc[0]=np.nan
      >>> d
      col1 col2 col3
      0 1 NaN 5
      1 2 4.0 6
      >>> d['col1'].iloc[1]=np.nan
      >>> d
      col1 col2 col3
      0 1.0 NaN 5
      1 NaN 4.0 6
      >>> d['col3'].iloc[1]=np.nan
      >>> d
      col1 col2 col3
      0 1.0 NaN 5.0
      1 NaN 4.0 NaN


      Now, I would like the column addition to have the following output:



      >>> d['col1']+d['col3']
      0 6.0
      1 NaN

      >>> d['col1']+d['col2']
      0 1.0
      1 4.0


      However, in reality, the output is instead:



      >>> d['col1']+d['col3']
      0 6.0
      1 NaN

      >>> d['col1']+d['col2']
      0 NaN
      1 NaN


      Anyone knows how to achieve this?







      python pandas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 22:58









      Jinhua WangJinhua Wang

      528416




      528416
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You can use add to get your sums, with fill_value=0:



          >>> d.col1.add(d.col2, fill_value=0)
          0 1.0
          1 4.0
          dtype: float64

          >>> d.col1.add(d.col3, fill_value=0)
          0 6.0
          1 NaN
          dtype: float64





          share|improve this answer































            2














            When adding columns one and two, use Series.add with fill_value=0.



            >>> d
            col1 col2 col3
            0 1.0 NaN 5.0
            1 NaN 4.0 NaN
            >>>
            >>> d['col1'].add(d['col2'], fill_value=0)
            0 1.0
            1 4.0
            dtype: float64


            Dataframes and series have methods like add, sub, ... in order to perform more sophisticated operations than the associated operators +, -, ... can provide.



            The methods may take additional arguments that finetune the operation.






            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',
              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%2f53463101%2fpandas-summing-two-columns-with-nan%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              You can use add to get your sums, with fill_value=0:



              >>> d.col1.add(d.col2, fill_value=0)
              0 1.0
              1 4.0
              dtype: float64

              >>> d.col1.add(d.col3, fill_value=0)
              0 6.0
              1 NaN
              dtype: float64





              share|improve this answer




























                1














                You can use add to get your sums, with fill_value=0:



                >>> d.col1.add(d.col2, fill_value=0)
                0 1.0
                1 4.0
                dtype: float64

                >>> d.col1.add(d.col3, fill_value=0)
                0 6.0
                1 NaN
                dtype: float64





                share|improve this answer


























                  1












                  1








                  1







                  You can use add to get your sums, with fill_value=0:



                  >>> d.col1.add(d.col2, fill_value=0)
                  0 1.0
                  1 4.0
                  dtype: float64

                  >>> d.col1.add(d.col3, fill_value=0)
                  0 6.0
                  1 NaN
                  dtype: float64





                  share|improve this answer













                  You can use add to get your sums, with fill_value=0:



                  >>> d.col1.add(d.col2, fill_value=0)
                  0 1.0
                  1 4.0
                  dtype: float64

                  >>> d.col1.add(d.col3, fill_value=0)
                  0 6.0
                  1 NaN
                  dtype: float64






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 23:02









                  sacuLsacuL

                  30.5k41943




                  30.5k41943

























                      2














                      When adding columns one and two, use Series.add with fill_value=0.



                      >>> d
                      col1 col2 col3
                      0 1.0 NaN 5.0
                      1 NaN 4.0 NaN
                      >>>
                      >>> d['col1'].add(d['col2'], fill_value=0)
                      0 1.0
                      1 4.0
                      dtype: float64


                      Dataframes and series have methods like add, sub, ... in order to perform more sophisticated operations than the associated operators +, -, ... can provide.



                      The methods may take additional arguments that finetune the operation.






                      share|improve this answer






























                        2














                        When adding columns one and two, use Series.add with fill_value=0.



                        >>> d
                        col1 col2 col3
                        0 1.0 NaN 5.0
                        1 NaN 4.0 NaN
                        >>>
                        >>> d['col1'].add(d['col2'], fill_value=0)
                        0 1.0
                        1 4.0
                        dtype: float64


                        Dataframes and series have methods like add, sub, ... in order to perform more sophisticated operations than the associated operators +, -, ... can provide.



                        The methods may take additional arguments that finetune the operation.






                        share|improve this answer




























                          2












                          2








                          2







                          When adding columns one and two, use Series.add with fill_value=0.



                          >>> d
                          col1 col2 col3
                          0 1.0 NaN 5.0
                          1 NaN 4.0 NaN
                          >>>
                          >>> d['col1'].add(d['col2'], fill_value=0)
                          0 1.0
                          1 4.0
                          dtype: float64


                          Dataframes and series have methods like add, sub, ... in order to perform more sophisticated operations than the associated operators +, -, ... can provide.



                          The methods may take additional arguments that finetune the operation.






                          share|improve this answer















                          When adding columns one and two, use Series.add with fill_value=0.



                          >>> d
                          col1 col2 col3
                          0 1.0 NaN 5.0
                          1 NaN 4.0 NaN
                          >>>
                          >>> d['col1'].add(d['col2'], fill_value=0)
                          0 1.0
                          1 4.0
                          dtype: float64


                          Dataframes and series have methods like add, sub, ... in order to perform more sophisticated operations than the associated operators +, -, ... can provide.



                          The methods may take additional arguments that finetune the operation.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 24 '18 at 23:14

























                          answered Nov 24 '18 at 23:02









                          timgebtimgeb

                          51.1k116693




                          51.1k116693






























                              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%2f53463101%2fpandas-summing-two-columns-with-nan%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

                              Ottavio Pratesi

                              Tricia Helfer

                              15 giugno