Combine two Pandas series?












1















I have two Pandas Series, s1 and s2 that I would like to combine.



s1 = pd.Series([2,5,5], index=['a','b','c'])
s2 = pd.Series([2,4,7], index=['a','b','d'])


This is the result I would like:



s3 = pd.Series([4,9,5,7], index=['a','b','c','d'])

a 4
b 9
c 5
d 7
dtype: int64


I've tried the + operator, s1.append(s2), and pd.join([s1,s2]) but these don't give the result I'm looking for.










share|improve this question


















  • 1





    How about s1.add(s2)?

    – Anton vBR
    Nov 20 '18 at 17:07
















1















I have two Pandas Series, s1 and s2 that I would like to combine.



s1 = pd.Series([2,5,5], index=['a','b','c'])
s2 = pd.Series([2,4,7], index=['a','b','d'])


This is the result I would like:



s3 = pd.Series([4,9,5,7], index=['a','b','c','d'])

a 4
b 9
c 5
d 7
dtype: int64


I've tried the + operator, s1.append(s2), and pd.join([s1,s2]) but these don't give the result I'm looking for.










share|improve this question


















  • 1





    How about s1.add(s2)?

    – Anton vBR
    Nov 20 '18 at 17:07














1












1








1








I have two Pandas Series, s1 and s2 that I would like to combine.



s1 = pd.Series([2,5,5], index=['a','b','c'])
s2 = pd.Series([2,4,7], index=['a','b','d'])


This is the result I would like:



s3 = pd.Series([4,9,5,7], index=['a','b','c','d'])

a 4
b 9
c 5
d 7
dtype: int64


I've tried the + operator, s1.append(s2), and pd.join([s1,s2]) but these don't give the result I'm looking for.










share|improve this question














I have two Pandas Series, s1 and s2 that I would like to combine.



s1 = pd.Series([2,5,5], index=['a','b','c'])
s2 = pd.Series([2,4,7], index=['a','b','d'])


This is the result I would like:



s3 = pd.Series([4,9,5,7], index=['a','b','c','d'])

a 4
b 9
c 5
d 7
dtype: int64


I've tried the + operator, s1.append(s2), and pd.join([s1,s2]) but these don't give the result I'm looking for.







python pandas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 17:04









maxmax

480113




480113








  • 1





    How about s1.add(s2)?

    – Anton vBR
    Nov 20 '18 at 17:07














  • 1





    How about s1.add(s2)?

    – Anton vBR
    Nov 20 '18 at 17:07








1




1





How about s1.add(s2)?

– Anton vBR
Nov 20 '18 at 17:07





How about s1.add(s2)?

– Anton vBR
Nov 20 '18 at 17:07












5 Answers
5






active

oldest

votes


















5














Seems like add will work here



s1.add(s2,fill_value=0)
Out[145]:
a 4.0
b 9.0
c 5.0
d 7.0
dtype: float64





share|improve this answer































    1














    Numpy np.add.at



    This is kind of ridiculous but I wanted to show something different



    import numpy as np
    import pandas as pd
    from itertools import chain

    k, v = zip(*chain(*map(pd.Series.items, [s1, s2])))
    i, r = pd.factorize(k)
    out = np.zeros(len(r), dtype=int)
    np.add.at(out, i, v)
    pd.Series(out, r)

    a 4
    b 9
    c 5
    d 7
    dtype: int64





    share|improve this answer



















    • 1





      This is good but seems way lengthy when pandas DataFrame.add is there :-)

      – pygo
      Nov 20 '18 at 17:29











    • @pygo hence the comment ridiculous. I personally believe it serves to show how it could be done differently. If people see these alternative answers, they may incorporate the knowledge and find better uses for it later.

      – piRSquared
      Nov 20 '18 at 17:33











    • of course that's good for ppls over SO for looking such logics, thnx.

      – pygo
      Nov 20 '18 at 17:35



















    0














    Just use pandas.Series.add



    s3 = s1.add(s2, fill_value=0)

    #result
    a 4.0
    b 9.0
    c 5.0
    d 7.0
    dtype: float64





    share|improve this answer































      0














      Alternative to add



      pd.concat((s1,s2), axis=1, sort=True).sum(1, min_count=1)


      Output:



      a    4.0
      b 9.0
      c 5.0
      d 7.0
      dtype: float64





      share|improve this answer































        0














        There is simple answer Directly from pandas.Series.add , you will get it from here



        For you Use case here..



        First dataframe:



        >>> s1
        a 2
        b 4
        c 5
        dtype: int64


        Second Dataframe:



        >>> s2
        a 2
        b 4
        d 7
        dtype: int64


        Simple DataFrame.add + fill_value which is explained in docs:



        >>> s1.add(s2, fill_value=0)
        a 4.0
        b 8.0
        c 5.0
        d 7.0
        dtype: float64


        Document :




        fill_value : None or float value, default None



        Fill existing missing (NaN) values, and any new element needed for
        successful DataFrame alignment, with this value before computation. If
        data in both corresponding DataFrame locations is missing the result
        will be missing







        share|improve this answer


























        • These are Series so the correct method is pandas.Series.add. Also, this is addressed by @W-B

          – piRSquared
          Nov 20 '18 at 17:35











        • Thnx for pointing that out , link is just stand corrected , i Know its addressed by @W-B and Drew Nicolette but thought to link that with original docs while showing that for the use case here, i'm firm believer in getting & putting the explanation and like that style because it helps newbies to understand under the hood :).

          – pygo
          Nov 20 '18 at 17:43













        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%2f53398008%2fcombine-two-pandas-series%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        5














        Seems like add will work here



        s1.add(s2,fill_value=0)
        Out[145]:
        a 4.0
        b 9.0
        c 5.0
        d 7.0
        dtype: float64





        share|improve this answer




























          5














          Seems like add will work here



          s1.add(s2,fill_value=0)
          Out[145]:
          a 4.0
          b 9.0
          c 5.0
          d 7.0
          dtype: float64





          share|improve this answer


























            5












            5








            5







            Seems like add will work here



            s1.add(s2,fill_value=0)
            Out[145]:
            a 4.0
            b 9.0
            c 5.0
            d 7.0
            dtype: float64





            share|improve this answer













            Seems like add will work here



            s1.add(s2,fill_value=0)
            Out[145]:
            a 4.0
            b 9.0
            c 5.0
            d 7.0
            dtype: float64






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 17:07









            W-BW-B

            106k83165




            106k83165

























                1














                Numpy np.add.at



                This is kind of ridiculous but I wanted to show something different



                import numpy as np
                import pandas as pd
                from itertools import chain

                k, v = zip(*chain(*map(pd.Series.items, [s1, s2])))
                i, r = pd.factorize(k)
                out = np.zeros(len(r), dtype=int)
                np.add.at(out, i, v)
                pd.Series(out, r)

                a 4
                b 9
                c 5
                d 7
                dtype: int64





                share|improve this answer



















                • 1





                  This is good but seems way lengthy when pandas DataFrame.add is there :-)

                  – pygo
                  Nov 20 '18 at 17:29











                • @pygo hence the comment ridiculous. I personally believe it serves to show how it could be done differently. If people see these alternative answers, they may incorporate the knowledge and find better uses for it later.

                  – piRSquared
                  Nov 20 '18 at 17:33











                • of course that's good for ppls over SO for looking such logics, thnx.

                  – pygo
                  Nov 20 '18 at 17:35
















                1














                Numpy np.add.at



                This is kind of ridiculous but I wanted to show something different



                import numpy as np
                import pandas as pd
                from itertools import chain

                k, v = zip(*chain(*map(pd.Series.items, [s1, s2])))
                i, r = pd.factorize(k)
                out = np.zeros(len(r), dtype=int)
                np.add.at(out, i, v)
                pd.Series(out, r)

                a 4
                b 9
                c 5
                d 7
                dtype: int64





                share|improve this answer



















                • 1





                  This is good but seems way lengthy when pandas DataFrame.add is there :-)

                  – pygo
                  Nov 20 '18 at 17:29











                • @pygo hence the comment ridiculous. I personally believe it serves to show how it could be done differently. If people see these alternative answers, they may incorporate the knowledge and find better uses for it later.

                  – piRSquared
                  Nov 20 '18 at 17:33











                • of course that's good for ppls over SO for looking such logics, thnx.

                  – pygo
                  Nov 20 '18 at 17:35














                1












                1








                1







                Numpy np.add.at



                This is kind of ridiculous but I wanted to show something different



                import numpy as np
                import pandas as pd
                from itertools import chain

                k, v = zip(*chain(*map(pd.Series.items, [s1, s2])))
                i, r = pd.factorize(k)
                out = np.zeros(len(r), dtype=int)
                np.add.at(out, i, v)
                pd.Series(out, r)

                a 4
                b 9
                c 5
                d 7
                dtype: int64





                share|improve this answer













                Numpy np.add.at



                This is kind of ridiculous but I wanted to show something different



                import numpy as np
                import pandas as pd
                from itertools import chain

                k, v = zip(*chain(*map(pd.Series.items, [s1, s2])))
                i, r = pd.factorize(k)
                out = np.zeros(len(r), dtype=int)
                np.add.at(out, i, v)
                pd.Series(out, r)

                a 4
                b 9
                c 5
                d 7
                dtype: int64






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 '18 at 17:23









                piRSquaredpiRSquared

                153k22144287




                153k22144287








                • 1





                  This is good but seems way lengthy when pandas DataFrame.add is there :-)

                  – pygo
                  Nov 20 '18 at 17:29











                • @pygo hence the comment ridiculous. I personally believe it serves to show how it could be done differently. If people see these alternative answers, they may incorporate the knowledge and find better uses for it later.

                  – piRSquared
                  Nov 20 '18 at 17:33











                • of course that's good for ppls over SO for looking such logics, thnx.

                  – pygo
                  Nov 20 '18 at 17:35














                • 1





                  This is good but seems way lengthy when pandas DataFrame.add is there :-)

                  – pygo
                  Nov 20 '18 at 17:29











                • @pygo hence the comment ridiculous. I personally believe it serves to show how it could be done differently. If people see these alternative answers, they may incorporate the knowledge and find better uses for it later.

                  – piRSquared
                  Nov 20 '18 at 17:33











                • of course that's good for ppls over SO for looking such logics, thnx.

                  – pygo
                  Nov 20 '18 at 17:35








                1




                1





                This is good but seems way lengthy when pandas DataFrame.add is there :-)

                – pygo
                Nov 20 '18 at 17:29





                This is good but seems way lengthy when pandas DataFrame.add is there :-)

                – pygo
                Nov 20 '18 at 17:29













                @pygo hence the comment ridiculous. I personally believe it serves to show how it could be done differently. If people see these alternative answers, they may incorporate the knowledge and find better uses for it later.

                – piRSquared
                Nov 20 '18 at 17:33





                @pygo hence the comment ridiculous. I personally believe it serves to show how it could be done differently. If people see these alternative answers, they may incorporate the knowledge and find better uses for it later.

                – piRSquared
                Nov 20 '18 at 17:33













                of course that's good for ppls over SO for looking such logics, thnx.

                – pygo
                Nov 20 '18 at 17:35





                of course that's good for ppls over SO for looking such logics, thnx.

                – pygo
                Nov 20 '18 at 17:35











                0














                Just use pandas.Series.add



                s3 = s1.add(s2, fill_value=0)

                #result
                a 4.0
                b 9.0
                c 5.0
                d 7.0
                dtype: float64





                share|improve this answer




























                  0














                  Just use pandas.Series.add



                  s3 = s1.add(s2, fill_value=0)

                  #result
                  a 4.0
                  b 9.0
                  c 5.0
                  d 7.0
                  dtype: float64





                  share|improve this answer


























                    0












                    0








                    0







                    Just use pandas.Series.add



                    s3 = s1.add(s2, fill_value=0)

                    #result
                    a 4.0
                    b 9.0
                    c 5.0
                    d 7.0
                    dtype: float64





                    share|improve this answer













                    Just use pandas.Series.add



                    s3 = s1.add(s2, fill_value=0)

                    #result
                    a 4.0
                    b 9.0
                    c 5.0
                    d 7.0
                    dtype: float64






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 20 '18 at 17:11









                    Drew NicoletteDrew Nicolette

                    1063




                    1063























                        0














                        Alternative to add



                        pd.concat((s1,s2), axis=1, sort=True).sum(1, min_count=1)


                        Output:



                        a    4.0
                        b 9.0
                        c 5.0
                        d 7.0
                        dtype: float64





                        share|improve this answer




























                          0














                          Alternative to add



                          pd.concat((s1,s2), axis=1, sort=True).sum(1, min_count=1)


                          Output:



                          a    4.0
                          b 9.0
                          c 5.0
                          d 7.0
                          dtype: float64





                          share|improve this answer


























                            0












                            0








                            0







                            Alternative to add



                            pd.concat((s1,s2), axis=1, sort=True).sum(1, min_count=1)


                            Output:



                            a    4.0
                            b 9.0
                            c 5.0
                            d 7.0
                            dtype: float64





                            share|improve this answer













                            Alternative to add



                            pd.concat((s1,s2), axis=1, sort=True).sum(1, min_count=1)


                            Output:



                            a    4.0
                            b 9.0
                            c 5.0
                            d 7.0
                            dtype: float64






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 20 '18 at 17:11









                            Srce CdeSrce Cde

                            1,144511




                            1,144511























                                0














                                There is simple answer Directly from pandas.Series.add , you will get it from here



                                For you Use case here..



                                First dataframe:



                                >>> s1
                                a 2
                                b 4
                                c 5
                                dtype: int64


                                Second Dataframe:



                                >>> s2
                                a 2
                                b 4
                                d 7
                                dtype: int64


                                Simple DataFrame.add + fill_value which is explained in docs:



                                >>> s1.add(s2, fill_value=0)
                                a 4.0
                                b 8.0
                                c 5.0
                                d 7.0
                                dtype: float64


                                Document :




                                fill_value : None or float value, default None



                                Fill existing missing (NaN) values, and any new element needed for
                                successful DataFrame alignment, with this value before computation. If
                                data in both corresponding DataFrame locations is missing the result
                                will be missing







                                share|improve this answer


























                                • These are Series so the correct method is pandas.Series.add. Also, this is addressed by @W-B

                                  – piRSquared
                                  Nov 20 '18 at 17:35











                                • Thnx for pointing that out , link is just stand corrected , i Know its addressed by @W-B and Drew Nicolette but thought to link that with original docs while showing that for the use case here, i'm firm believer in getting & putting the explanation and like that style because it helps newbies to understand under the hood :).

                                  – pygo
                                  Nov 20 '18 at 17:43


















                                0














                                There is simple answer Directly from pandas.Series.add , you will get it from here



                                For you Use case here..



                                First dataframe:



                                >>> s1
                                a 2
                                b 4
                                c 5
                                dtype: int64


                                Second Dataframe:



                                >>> s2
                                a 2
                                b 4
                                d 7
                                dtype: int64


                                Simple DataFrame.add + fill_value which is explained in docs:



                                >>> s1.add(s2, fill_value=0)
                                a 4.0
                                b 8.0
                                c 5.0
                                d 7.0
                                dtype: float64


                                Document :




                                fill_value : None or float value, default None



                                Fill existing missing (NaN) values, and any new element needed for
                                successful DataFrame alignment, with this value before computation. If
                                data in both corresponding DataFrame locations is missing the result
                                will be missing







                                share|improve this answer


























                                • These are Series so the correct method is pandas.Series.add. Also, this is addressed by @W-B

                                  – piRSquared
                                  Nov 20 '18 at 17:35











                                • Thnx for pointing that out , link is just stand corrected , i Know its addressed by @W-B and Drew Nicolette but thought to link that with original docs while showing that for the use case here, i'm firm believer in getting & putting the explanation and like that style because it helps newbies to understand under the hood :).

                                  – pygo
                                  Nov 20 '18 at 17:43
















                                0












                                0








                                0







                                There is simple answer Directly from pandas.Series.add , you will get it from here



                                For you Use case here..



                                First dataframe:



                                >>> s1
                                a 2
                                b 4
                                c 5
                                dtype: int64


                                Second Dataframe:



                                >>> s2
                                a 2
                                b 4
                                d 7
                                dtype: int64


                                Simple DataFrame.add + fill_value which is explained in docs:



                                >>> s1.add(s2, fill_value=0)
                                a 4.0
                                b 8.0
                                c 5.0
                                d 7.0
                                dtype: float64


                                Document :




                                fill_value : None or float value, default None



                                Fill existing missing (NaN) values, and any new element needed for
                                successful DataFrame alignment, with this value before computation. If
                                data in both corresponding DataFrame locations is missing the result
                                will be missing







                                share|improve this answer















                                There is simple answer Directly from pandas.Series.add , you will get it from here



                                For you Use case here..



                                First dataframe:



                                >>> s1
                                a 2
                                b 4
                                c 5
                                dtype: int64


                                Second Dataframe:



                                >>> s2
                                a 2
                                b 4
                                d 7
                                dtype: int64


                                Simple DataFrame.add + fill_value which is explained in docs:



                                >>> s1.add(s2, fill_value=0)
                                a 4.0
                                b 8.0
                                c 5.0
                                d 7.0
                                dtype: float64


                                Document :




                                fill_value : None or float value, default None



                                Fill existing missing (NaN) values, and any new element needed for
                                successful DataFrame alignment, with this value before computation. If
                                data in both corresponding DataFrame locations is missing the result
                                will be missing








                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 21 '18 at 3:20

























                                answered Nov 20 '18 at 17:24









                                pygopygo

                                2,6681619




                                2,6681619













                                • These are Series so the correct method is pandas.Series.add. Also, this is addressed by @W-B

                                  – piRSquared
                                  Nov 20 '18 at 17:35











                                • Thnx for pointing that out , link is just stand corrected , i Know its addressed by @W-B and Drew Nicolette but thought to link that with original docs while showing that for the use case here, i'm firm believer in getting & putting the explanation and like that style because it helps newbies to understand under the hood :).

                                  – pygo
                                  Nov 20 '18 at 17:43





















                                • These are Series so the correct method is pandas.Series.add. Also, this is addressed by @W-B

                                  – piRSquared
                                  Nov 20 '18 at 17:35











                                • Thnx for pointing that out , link is just stand corrected , i Know its addressed by @W-B and Drew Nicolette but thought to link that with original docs while showing that for the use case here, i'm firm believer in getting & putting the explanation and like that style because it helps newbies to understand under the hood :).

                                  – pygo
                                  Nov 20 '18 at 17:43



















                                These are Series so the correct method is pandas.Series.add. Also, this is addressed by @W-B

                                – piRSquared
                                Nov 20 '18 at 17:35





                                These are Series so the correct method is pandas.Series.add. Also, this is addressed by @W-B

                                – piRSquared
                                Nov 20 '18 at 17:35













                                Thnx for pointing that out , link is just stand corrected , i Know its addressed by @W-B and Drew Nicolette but thought to link that with original docs while showing that for the use case here, i'm firm believer in getting & putting the explanation and like that style because it helps newbies to understand under the hood :).

                                – pygo
                                Nov 20 '18 at 17:43







                                Thnx for pointing that out , link is just stand corrected , i Know its addressed by @W-B and Drew Nicolette but thought to link that with original docs while showing that for the use case here, i'm firm believer in getting & putting the explanation and like that style because it helps newbies to understand under the hood :).

                                – pygo
                                Nov 20 '18 at 17:43




















                                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%2f53398008%2fcombine-two-pandas-series%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

                                If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                                Alcedinidae

                                Can an atomic nucleus contain both particles and antiparticles? [duplicate]