Adding values of two Pandas series with different column names












1














I have two pandas series of the same length but with different column names. How can one add the values in them?



series.add(other, fill_value=0, axis=0) does avoid NaN-values, but the values are not added. Instead, the result is a concatenation of the two series.



Is there a way to obtain a new series consisting of the sum of the values in two series?










share|improve this question





























    1














    I have two pandas series of the same length but with different column names. How can one add the values in them?



    series.add(other, fill_value=0, axis=0) does avoid NaN-values, but the values are not added. Instead, the result is a concatenation of the two series.



    Is there a way to obtain a new series consisting of the sum of the values in two series?










    share|improve this question



























      1












      1








      1







      I have two pandas series of the same length but with different column names. How can one add the values in them?



      series.add(other, fill_value=0, axis=0) does avoid NaN-values, but the values are not added. Instead, the result is a concatenation of the two series.



      Is there a way to obtain a new series consisting of the sum of the values in two series?










      share|improve this question















      I have two pandas series of the same length but with different column names. How can one add the values in them?



      series.add(other, fill_value=0, axis=0) does avoid NaN-values, but the values are not added. Instead, the result is a concatenation of the two series.



      Is there a way to obtain a new series consisting of the sum of the values in two series?







      python pandas indexing series






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 20:27









      jpp

      93.2k2054104




      93.2k2054104










      asked Nov 23 '18 at 20:12









      Sebastian AllardSebastian Allard

      84




      84
























          2 Answers
          2






          active

          oldest

          votes


















          1














          The values attribute lets you access the underlying raw numpy arrays. You can add those.



          raw_sum = series.values + other.values
          series2 = Series(raw_sum, index=series.index)


          This also works:



          series2 = series + other.values





          share|improve this answer





















          • It's worth noting that in general indices have meanings, even if they just boil down to "row numbers". If your 2 series are meant to be added but you have mismatched indices, it's worth finding out why.
            – jpp
            Nov 23 '18 at 20:28












          • Thank you! Simple solution :) The series consisted of a sport teams average home and away statistics. The columns for home statistics were named statistic_home and the corresponding away statistics were named away_statistic.
            – Sebastian Allard
            Nov 23 '18 at 20:28





















          1














          Mismatched indidces



          This issue is your 2 series have different indices. Here's an example:



          s1 = pd.Series([1, np.nan, 3, np.nan, 5], index=np.arange(5))
          s2 = pd.Series([np.nan, 7, 8, np.nan, np.nan], index=np.arange(5)+10)

          print(s1.add(s2, fill_value=0, axis=0))

          0 1.0
          1 NaN
          2 3.0
          3 NaN
          4 5.0
          10 NaN
          11 7.0
          12 8.0
          13 NaN
          14 NaN
          dtype: float64


          You have 2 options: reindex via, for example, a dictionary or disregard indices and add your series positionally.



          Map index of one series to align with the other



          You can use a dictionary to realign. The mapping below is arbitrary. NaN values occur where, after reindexing, values in both series are NaN:



          index_map = dict(zip(np.arange(5) + 10, [3, 2, 4, 0, 1]))
          s2.index = s2.index.map(index_map)

          print(s1.add(s2, fill_value=0, axis=0))

          0 1.0
          1 NaN
          2 10.0
          3 NaN
          4 13.0
          dtype: float64


          Disregard indices; use positional location only



          In this case, you can either construct a new series with the regular pd.RangeIndex as index (i.e. 0, 1, 2, ...), or use an index from one of the input series:



          # normalized index
          res = pd.Series(s1.values + s2.values)

          # take index from s1
          res = pd.Series(s1.values + s2.values, index=s1.index)





          share|improve this answer























          • Thank you for a detailed solution :)
            – Sebastian Allard
            Nov 23 '18 at 20:35











          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%2f53452464%2fadding-values-of-two-pandas-series-with-different-column-names%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














          The values attribute lets you access the underlying raw numpy arrays. You can add those.



          raw_sum = series.values + other.values
          series2 = Series(raw_sum, index=series.index)


          This also works:



          series2 = series + other.values





          share|improve this answer





















          • It's worth noting that in general indices have meanings, even if they just boil down to "row numbers". If your 2 series are meant to be added but you have mismatched indices, it's worth finding out why.
            – jpp
            Nov 23 '18 at 20:28












          • Thank you! Simple solution :) The series consisted of a sport teams average home and away statistics. The columns for home statistics were named statistic_home and the corresponding away statistics were named away_statistic.
            – Sebastian Allard
            Nov 23 '18 at 20:28


















          1














          The values attribute lets you access the underlying raw numpy arrays. You can add those.



          raw_sum = series.values + other.values
          series2 = Series(raw_sum, index=series.index)


          This also works:



          series2 = series + other.values





          share|improve this answer





















          • It's worth noting that in general indices have meanings, even if they just boil down to "row numbers". If your 2 series are meant to be added but you have mismatched indices, it's worth finding out why.
            – jpp
            Nov 23 '18 at 20:28












          • Thank you! Simple solution :) The series consisted of a sport teams average home and away statistics. The columns for home statistics were named statistic_home and the corresponding away statistics were named away_statistic.
            – Sebastian Allard
            Nov 23 '18 at 20:28
















          1












          1








          1






          The values attribute lets you access the underlying raw numpy arrays. You can add those.



          raw_sum = series.values + other.values
          series2 = Series(raw_sum, index=series.index)


          This also works:



          series2 = series + other.values





          share|improve this answer












          The values attribute lets you access the underlying raw numpy arrays. You can add those.



          raw_sum = series.values + other.values
          series2 = Series(raw_sum, index=series.index)


          This also works:



          series2 = series + other.values






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 20:18









          shx2shx2

          40.3k679110




          40.3k679110












          • It's worth noting that in general indices have meanings, even if they just boil down to "row numbers". If your 2 series are meant to be added but you have mismatched indices, it's worth finding out why.
            – jpp
            Nov 23 '18 at 20:28












          • Thank you! Simple solution :) The series consisted of a sport teams average home and away statistics. The columns for home statistics were named statistic_home and the corresponding away statistics were named away_statistic.
            – Sebastian Allard
            Nov 23 '18 at 20:28




















          • It's worth noting that in general indices have meanings, even if they just boil down to "row numbers". If your 2 series are meant to be added but you have mismatched indices, it's worth finding out why.
            – jpp
            Nov 23 '18 at 20:28












          • Thank you! Simple solution :) The series consisted of a sport teams average home and away statistics. The columns for home statistics were named statistic_home and the corresponding away statistics were named away_statistic.
            – Sebastian Allard
            Nov 23 '18 at 20:28


















          It's worth noting that in general indices have meanings, even if they just boil down to "row numbers". If your 2 series are meant to be added but you have mismatched indices, it's worth finding out why.
          – jpp
          Nov 23 '18 at 20:28






          It's worth noting that in general indices have meanings, even if they just boil down to "row numbers". If your 2 series are meant to be added but you have mismatched indices, it's worth finding out why.
          – jpp
          Nov 23 '18 at 20:28














          Thank you! Simple solution :) The series consisted of a sport teams average home and away statistics. The columns for home statistics were named statistic_home and the corresponding away statistics were named away_statistic.
          – Sebastian Allard
          Nov 23 '18 at 20:28






          Thank you! Simple solution :) The series consisted of a sport teams average home and away statistics. The columns for home statistics were named statistic_home and the corresponding away statistics were named away_statistic.
          – Sebastian Allard
          Nov 23 '18 at 20:28















          1














          Mismatched indidces



          This issue is your 2 series have different indices. Here's an example:



          s1 = pd.Series([1, np.nan, 3, np.nan, 5], index=np.arange(5))
          s2 = pd.Series([np.nan, 7, 8, np.nan, np.nan], index=np.arange(5)+10)

          print(s1.add(s2, fill_value=0, axis=0))

          0 1.0
          1 NaN
          2 3.0
          3 NaN
          4 5.0
          10 NaN
          11 7.0
          12 8.0
          13 NaN
          14 NaN
          dtype: float64


          You have 2 options: reindex via, for example, a dictionary or disregard indices and add your series positionally.



          Map index of one series to align with the other



          You can use a dictionary to realign. The mapping below is arbitrary. NaN values occur where, after reindexing, values in both series are NaN:



          index_map = dict(zip(np.arange(5) + 10, [3, 2, 4, 0, 1]))
          s2.index = s2.index.map(index_map)

          print(s1.add(s2, fill_value=0, axis=0))

          0 1.0
          1 NaN
          2 10.0
          3 NaN
          4 13.0
          dtype: float64


          Disregard indices; use positional location only



          In this case, you can either construct a new series with the regular pd.RangeIndex as index (i.e. 0, 1, 2, ...), or use an index from one of the input series:



          # normalized index
          res = pd.Series(s1.values + s2.values)

          # take index from s1
          res = pd.Series(s1.values + s2.values, index=s1.index)





          share|improve this answer























          • Thank you for a detailed solution :)
            – Sebastian Allard
            Nov 23 '18 at 20:35
















          1














          Mismatched indidces



          This issue is your 2 series have different indices. Here's an example:



          s1 = pd.Series([1, np.nan, 3, np.nan, 5], index=np.arange(5))
          s2 = pd.Series([np.nan, 7, 8, np.nan, np.nan], index=np.arange(5)+10)

          print(s1.add(s2, fill_value=0, axis=0))

          0 1.0
          1 NaN
          2 3.0
          3 NaN
          4 5.0
          10 NaN
          11 7.0
          12 8.0
          13 NaN
          14 NaN
          dtype: float64


          You have 2 options: reindex via, for example, a dictionary or disregard indices and add your series positionally.



          Map index of one series to align with the other



          You can use a dictionary to realign. The mapping below is arbitrary. NaN values occur where, after reindexing, values in both series are NaN:



          index_map = dict(zip(np.arange(5) + 10, [3, 2, 4, 0, 1]))
          s2.index = s2.index.map(index_map)

          print(s1.add(s2, fill_value=0, axis=0))

          0 1.0
          1 NaN
          2 10.0
          3 NaN
          4 13.0
          dtype: float64


          Disregard indices; use positional location only



          In this case, you can either construct a new series with the regular pd.RangeIndex as index (i.e. 0, 1, 2, ...), or use an index from one of the input series:



          # normalized index
          res = pd.Series(s1.values + s2.values)

          # take index from s1
          res = pd.Series(s1.values + s2.values, index=s1.index)





          share|improve this answer























          • Thank you for a detailed solution :)
            – Sebastian Allard
            Nov 23 '18 at 20:35














          1












          1








          1






          Mismatched indidces



          This issue is your 2 series have different indices. Here's an example:



          s1 = pd.Series([1, np.nan, 3, np.nan, 5], index=np.arange(5))
          s2 = pd.Series([np.nan, 7, 8, np.nan, np.nan], index=np.arange(5)+10)

          print(s1.add(s2, fill_value=0, axis=0))

          0 1.0
          1 NaN
          2 3.0
          3 NaN
          4 5.0
          10 NaN
          11 7.0
          12 8.0
          13 NaN
          14 NaN
          dtype: float64


          You have 2 options: reindex via, for example, a dictionary or disregard indices and add your series positionally.



          Map index of one series to align with the other



          You can use a dictionary to realign. The mapping below is arbitrary. NaN values occur where, after reindexing, values in both series are NaN:



          index_map = dict(zip(np.arange(5) + 10, [3, 2, 4, 0, 1]))
          s2.index = s2.index.map(index_map)

          print(s1.add(s2, fill_value=0, axis=0))

          0 1.0
          1 NaN
          2 10.0
          3 NaN
          4 13.0
          dtype: float64


          Disregard indices; use positional location only



          In this case, you can either construct a new series with the regular pd.RangeIndex as index (i.e. 0, 1, 2, ...), or use an index from one of the input series:



          # normalized index
          res = pd.Series(s1.values + s2.values)

          # take index from s1
          res = pd.Series(s1.values + s2.values, index=s1.index)





          share|improve this answer














          Mismatched indidces



          This issue is your 2 series have different indices. Here's an example:



          s1 = pd.Series([1, np.nan, 3, np.nan, 5], index=np.arange(5))
          s2 = pd.Series([np.nan, 7, 8, np.nan, np.nan], index=np.arange(5)+10)

          print(s1.add(s2, fill_value=0, axis=0))

          0 1.0
          1 NaN
          2 3.0
          3 NaN
          4 5.0
          10 NaN
          11 7.0
          12 8.0
          13 NaN
          14 NaN
          dtype: float64


          You have 2 options: reindex via, for example, a dictionary or disregard indices and add your series positionally.



          Map index of one series to align with the other



          You can use a dictionary to realign. The mapping below is arbitrary. NaN values occur where, after reindexing, values in both series are NaN:



          index_map = dict(zip(np.arange(5) + 10, [3, 2, 4, 0, 1]))
          s2.index = s2.index.map(index_map)

          print(s1.add(s2, fill_value=0, axis=0))

          0 1.0
          1 NaN
          2 10.0
          3 NaN
          4 13.0
          dtype: float64


          Disregard indices; use positional location only



          In this case, you can either construct a new series with the regular pd.RangeIndex as index (i.e. 0, 1, 2, ...), or use an index from one of the input series:



          # normalized index
          res = pd.Series(s1.values + s2.values)

          # take index from s1
          res = pd.Series(s1.values + s2.values, index=s1.index)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 20:24

























          answered Nov 23 '18 at 20:19









          jppjpp

          93.2k2054104




          93.2k2054104












          • Thank you for a detailed solution :)
            – Sebastian Allard
            Nov 23 '18 at 20:35


















          • Thank you for a detailed solution :)
            – Sebastian Allard
            Nov 23 '18 at 20:35
















          Thank you for a detailed solution :)
          – Sebastian Allard
          Nov 23 '18 at 20:35




          Thank you for a detailed solution :)
          – Sebastian Allard
          Nov 23 '18 at 20:35


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53452464%2fadding-values-of-two-pandas-series-with-different-column-names%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

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks