Merging two data frames into a new one with unique items marked with 1 or 0How to merge two dictionaries in a single expression?How to join (merge) data frames (inner, outer, left, right)Drop data frame columns by name“Large data” work flows using pandasMerging two data frames based on row values in python pandasMerge two python pandas data frames of different length but keep all rows in output data frameGet unmerged data from source tablesMerging two data frames with flexible conditionsCompare and equalize two data frames in PythonJoin two data frame with two columns values of a df with a single column values of another dataframe. based on some conditions?

Odd PCB Layout for Voltage Regulator

Why is quantum gravity non-renormalizable?

Searching for single buildings in QGIS

Disk usage confusion: 10G missing on Linux home partition on SSD

What's the idiomatic (or best) way to trim surrounding whitespace from a string?

To “Er” Is Human

Making arrow with a gradual colour

Why will we fail creating a self sustaining off world colony?

What type of education should I select in this form?

Variable declaration inside main loop

Square wave to sawtooth wave using two BJT

Why is the saxophone not common in classical repertoire?

When does it become illegal to exchange bitcoin for cash?

I agreed to cancel a long-planned vacation (with travel costs) due to project deadlines, but now the timeline has all changed again

What is the meaning of ゴト in the context of 鮎

Why did the Middle Kingdom stop building pyramid tombs?

Is this house-rule removing the increased effect of cantrips at higher character levels balanced?

2019 2-letters 33-length list

Does Dhp 256-257 condone judging others?

Why is my 401k manager recommending me to save more?

How soon after takeoff can you recline your airplane seat?

Old story where computer expert digitally animates The Lord of the Rings

SQL Server Ignoring Instance name when using port number of different instance

Is it OK to throw pebbles and stones in streams, waterfalls, ponds, etc.?



Merging two data frames into a new one with unique items marked with 1 or 0


How to merge two dictionaries in a single expression?How to join (merge) data frames (inner, outer, left, right)Drop data frame columns by name“Large data” work flows using pandasMerging two data frames based on row values in python pandasMerge two python pandas data frames of different length but keep all rows in output data frameGet unmerged data from source tablesMerging two data frames with flexible conditionsCompare and equalize two data frames in PythonJoin two data frame with two columns values of a df with a single column values of another dataframe. based on some conditions?













9















I have several dataframes.



Dataframe #1



Feature Coeff
a 0.5
b 0.3
c 0.35
d 0.2


Dataframe #2



Feature Coeff
a 0.7
b 0.2
y 0.75
x 0.1


I want to merge this dataframe and obtain the following one:



Feature | DF1 | DF2
a 1 1
b 1 1
c 1 0
d 1 0
y 0 1
x 0 1


I know that I can do an outer merge but I do not know how to move from there to obtain the final dataframe I presented above. Any ideas?










share|improve this question
























  • just to make sure: in the final DataFrame you don't need any Coeff?

    – Adam.Er8
    9 hours ago











  • @Adam.Er8 thanks! I do not need them. Thanks!

    – renakre
    9 hours ago















9















I have several dataframes.



Dataframe #1



Feature Coeff
a 0.5
b 0.3
c 0.35
d 0.2


Dataframe #2



Feature Coeff
a 0.7
b 0.2
y 0.75
x 0.1


I want to merge this dataframe and obtain the following one:



Feature | DF1 | DF2
a 1 1
b 1 1
c 1 0
d 1 0
y 0 1
x 0 1


I know that I can do an outer merge but I do not know how to move from there to obtain the final dataframe I presented above. Any ideas?










share|improve this question
























  • just to make sure: in the final DataFrame you don't need any Coeff?

    – Adam.Er8
    9 hours ago











  • @Adam.Er8 thanks! I do not need them. Thanks!

    – renakre
    9 hours ago













9












9








9


1






I have several dataframes.



Dataframe #1



Feature Coeff
a 0.5
b 0.3
c 0.35
d 0.2


Dataframe #2



Feature Coeff
a 0.7
b 0.2
y 0.75
x 0.1


I want to merge this dataframe and obtain the following one:



Feature | DF1 | DF2
a 1 1
b 1 1
c 1 0
d 1 0
y 0 1
x 0 1


I know that I can do an outer merge but I do not know how to move from there to obtain the final dataframe I presented above. Any ideas?










share|improve this question
















I have several dataframes.



Dataframe #1



Feature Coeff
a 0.5
b 0.3
c 0.35
d 0.2


Dataframe #2



Feature Coeff
a 0.7
b 0.2
y 0.75
x 0.1


I want to merge this dataframe and obtain the following one:



Feature | DF1 | DF2
a 1 1
b 1 1
c 1 0
d 1 0
y 0 1
x 0 1


I know that I can do an outer merge but I do not know how to move from there to obtain the final dataframe I presented above. Any ideas?







python pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 7 hours ago









cs95

157k26 gold badges208 silver badges279 bronze badges




157k26 gold badges208 silver badges279 bronze badges










asked 9 hours ago









renakrerenakre

5,3591 gold badge16 silver badges53 bronze badges




5,3591 gold badge16 silver badges53 bronze badges












  • just to make sure: in the final DataFrame you don't need any Coeff?

    – Adam.Er8
    9 hours ago











  • @Adam.Er8 thanks! I do not need them. Thanks!

    – renakre
    9 hours ago

















  • just to make sure: in the final DataFrame you don't need any Coeff?

    – Adam.Er8
    9 hours ago











  • @Adam.Er8 thanks! I do not need them. Thanks!

    – renakre
    9 hours ago
















just to make sure: in the final DataFrame you don't need any Coeff?

– Adam.Er8
9 hours ago





just to make sure: in the final DataFrame you don't need any Coeff?

– Adam.Er8
9 hours ago













@Adam.Er8 thanks! I do not need them. Thanks!

– renakre
9 hours ago





@Adam.Er8 thanks! I do not need them. Thanks!

– renakre
9 hours ago










4 Answers
4






active

oldest

votes


















8














Using concat+ get_dummies



u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])

pd.get_dummies(u.Feature).sum(level=0).T




 DF1 DF2
a 1 1
b 1 1
c 1 0
d 1 0
x 0 1
y 0 1





share|improve this answer






























    5














    You can use merge with series.str.get_dummies() together to achieve this:



    m=df1[['Feature']].merge(df2[['Feature']],how='outer',indicator=True)



    d='both':'DF1,DF2','left_only':'DF1','right_only':'DF2'
    m=m.assign(_merge=m._merge.map(d))
    m[['Feature']].join(m._merge.str.get_dummies(','))



     Feature DF1 DF2
    0 a 1 1
    1 b 1 1
    2 c 1 0
    3 d 1 0
    4 y 0 1
    5 x 0 1





    share|improve this answer






























      4














      Same Idea like user3483203 but with crosstab



      u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])


      pd.crosstab(u.Feature, u.index.get_level_values(0))





      share|improve this answer






























        2














        I merged two dataframes using pd.merge and used list comprehension to assign values.



        df = df1.merge(df2, on='Feature', how='outer')
        df['DF1'] = [1 if x > 0 else 0 for x in df['Coeff_x']]
        df['DF2'] = [1 if x > 0 else 0 for x in df['Coeff_y']]
        df.drop(['Coeff_x', 'Coeff_y'], axis=1, inplace=True)



         Feature DF1 DF2
        0 a 1 1
        1 b 1 1
        2 c 1 0
        3 d 1 0
        4 y 0 1
        5 x 0 1



        I've seen other -- pandas specific-- answers, and I would like to ask what are the advantages of methods like series.str.get_dummies() if you can achieve the same using built-in methods/functions? Is it much faster?
        Genuinely curious since I'm a newbie myself.



        (sorry I need more reputation points to leave comments directly under other answers!)






        share|improve this answer


















        • 1





          Not sure about performance, but you can assign the columns before the merge, then they get brought along df1.assign(DF1=1).merge(....) Just need a .fillna after :D

          – ALollz
          6 hours 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%2f56826251%2fmerging-two-data-frames-into-a-new-one-with-unique-items-marked-with-1-or-0%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        8














        Using concat+ get_dummies



        u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])

        pd.get_dummies(u.Feature).sum(level=0).T




         DF1 DF2
        a 1 1
        b 1 1
        c 1 0
        d 1 0
        x 0 1
        y 0 1





        share|improve this answer



























          8














          Using concat+ get_dummies



          u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])

          pd.get_dummies(u.Feature).sum(level=0).T




           DF1 DF2
          a 1 1
          b 1 1
          c 1 0
          d 1 0
          x 0 1
          y 0 1





          share|improve this answer

























            8












            8








            8







            Using concat+ get_dummies



            u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])

            pd.get_dummies(u.Feature).sum(level=0).T




             DF1 DF2
            a 1 1
            b 1 1
            c 1 0
            d 1 0
            x 0 1
            y 0 1





            share|improve this answer













            Using concat+ get_dummies



            u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])

            pd.get_dummies(u.Feature).sum(level=0).T




             DF1 DF2
            a 1 1
            b 1 1
            c 1 0
            d 1 0
            x 0 1
            y 0 1






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 9 hours ago









            user3483203user3483203

            35.1k8 gold badges31 silver badges60 bronze badges




            35.1k8 gold badges31 silver badges60 bronze badges





















                5














                You can use merge with series.str.get_dummies() together to achieve this:



                m=df1[['Feature']].merge(df2[['Feature']],how='outer',indicator=True)



                d='both':'DF1,DF2','left_only':'DF1','right_only':'DF2'
                m=m.assign(_merge=m._merge.map(d))
                m[['Feature']].join(m._merge.str.get_dummies(','))



                 Feature DF1 DF2
                0 a 1 1
                1 b 1 1
                2 c 1 0
                3 d 1 0
                4 y 0 1
                5 x 0 1





                share|improve this answer



























                  5














                  You can use merge with series.str.get_dummies() together to achieve this:



                  m=df1[['Feature']].merge(df2[['Feature']],how='outer',indicator=True)



                  d='both':'DF1,DF2','left_only':'DF1','right_only':'DF2'
                  m=m.assign(_merge=m._merge.map(d))
                  m[['Feature']].join(m._merge.str.get_dummies(','))



                   Feature DF1 DF2
                  0 a 1 1
                  1 b 1 1
                  2 c 1 0
                  3 d 1 0
                  4 y 0 1
                  5 x 0 1





                  share|improve this answer

























                    5












                    5








                    5







                    You can use merge with series.str.get_dummies() together to achieve this:



                    m=df1[['Feature']].merge(df2[['Feature']],how='outer',indicator=True)



                    d='both':'DF1,DF2','left_only':'DF1','right_only':'DF2'
                    m=m.assign(_merge=m._merge.map(d))
                    m[['Feature']].join(m._merge.str.get_dummies(','))



                     Feature DF1 DF2
                    0 a 1 1
                    1 b 1 1
                    2 c 1 0
                    3 d 1 0
                    4 y 0 1
                    5 x 0 1





                    share|improve this answer













                    You can use merge with series.str.get_dummies() together to achieve this:



                    m=df1[['Feature']].merge(df2[['Feature']],how='outer',indicator=True)



                    d='both':'DF1,DF2','left_only':'DF1','right_only':'DF2'
                    m=m.assign(_merge=m._merge.map(d))
                    m[['Feature']].join(m._merge.str.get_dummies(','))



                     Feature DF1 DF2
                    0 a 1 1
                    1 b 1 1
                    2 c 1 0
                    3 d 1 0
                    4 y 0 1
                    5 x 0 1






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 9 hours ago









                    anky_91anky_91

                    18.3k5 gold badges11 silver badges25 bronze badges




                    18.3k5 gold badges11 silver badges25 bronze badges





















                        4














                        Same Idea like user3483203 but with crosstab



                        u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])


                        pd.crosstab(u.Feature, u.index.get_level_values(0))





                        share|improve this answer



























                          4














                          Same Idea like user3483203 but with crosstab



                          u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])


                          pd.crosstab(u.Feature, u.index.get_level_values(0))





                          share|improve this answer

























                            4












                            4








                            4







                            Same Idea like user3483203 but with crosstab



                            u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])


                            pd.crosstab(u.Feature, u.index.get_level_values(0))





                            share|improve this answer













                            Same Idea like user3483203 but with crosstab



                            u = pd.concat([df1, df2], axis=0, keys=['DF1', 'DF2'])


                            pd.crosstab(u.Feature, u.index.get_level_values(0))






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 8 hours ago









                            WeNYoBenWeNYoBen

                            144k8 gold badges51 silver badges80 bronze badges




                            144k8 gold badges51 silver badges80 bronze badges





















                                2














                                I merged two dataframes using pd.merge and used list comprehension to assign values.



                                df = df1.merge(df2, on='Feature', how='outer')
                                df['DF1'] = [1 if x > 0 else 0 for x in df['Coeff_x']]
                                df['DF2'] = [1 if x > 0 else 0 for x in df['Coeff_y']]
                                df.drop(['Coeff_x', 'Coeff_y'], axis=1, inplace=True)



                                 Feature DF1 DF2
                                0 a 1 1
                                1 b 1 1
                                2 c 1 0
                                3 d 1 0
                                4 y 0 1
                                5 x 0 1



                                I've seen other -- pandas specific-- answers, and I would like to ask what are the advantages of methods like series.str.get_dummies() if you can achieve the same using built-in methods/functions? Is it much faster?
                                Genuinely curious since I'm a newbie myself.



                                (sorry I need more reputation points to leave comments directly under other answers!)






                                share|improve this answer


















                                • 1





                                  Not sure about performance, but you can assign the columns before the merge, then they get brought along df1.assign(DF1=1).merge(....) Just need a .fillna after :D

                                  – ALollz
                                  6 hours ago















                                2














                                I merged two dataframes using pd.merge and used list comprehension to assign values.



                                df = df1.merge(df2, on='Feature', how='outer')
                                df['DF1'] = [1 if x > 0 else 0 for x in df['Coeff_x']]
                                df['DF2'] = [1 if x > 0 else 0 for x in df['Coeff_y']]
                                df.drop(['Coeff_x', 'Coeff_y'], axis=1, inplace=True)



                                 Feature DF1 DF2
                                0 a 1 1
                                1 b 1 1
                                2 c 1 0
                                3 d 1 0
                                4 y 0 1
                                5 x 0 1



                                I've seen other -- pandas specific-- answers, and I would like to ask what are the advantages of methods like series.str.get_dummies() if you can achieve the same using built-in methods/functions? Is it much faster?
                                Genuinely curious since I'm a newbie myself.



                                (sorry I need more reputation points to leave comments directly under other answers!)






                                share|improve this answer


















                                • 1





                                  Not sure about performance, but you can assign the columns before the merge, then they get brought along df1.assign(DF1=1).merge(....) Just need a .fillna after :D

                                  – ALollz
                                  6 hours ago













                                2












                                2








                                2







                                I merged two dataframes using pd.merge and used list comprehension to assign values.



                                df = df1.merge(df2, on='Feature', how='outer')
                                df['DF1'] = [1 if x > 0 else 0 for x in df['Coeff_x']]
                                df['DF2'] = [1 if x > 0 else 0 for x in df['Coeff_y']]
                                df.drop(['Coeff_x', 'Coeff_y'], axis=1, inplace=True)



                                 Feature DF1 DF2
                                0 a 1 1
                                1 b 1 1
                                2 c 1 0
                                3 d 1 0
                                4 y 0 1
                                5 x 0 1



                                I've seen other -- pandas specific-- answers, and I would like to ask what are the advantages of methods like series.str.get_dummies() if you can achieve the same using built-in methods/functions? Is it much faster?
                                Genuinely curious since I'm a newbie myself.



                                (sorry I need more reputation points to leave comments directly under other answers!)






                                share|improve this answer













                                I merged two dataframes using pd.merge and used list comprehension to assign values.



                                df = df1.merge(df2, on='Feature', how='outer')
                                df['DF1'] = [1 if x > 0 else 0 for x in df['Coeff_x']]
                                df['DF2'] = [1 if x > 0 else 0 for x in df['Coeff_y']]
                                df.drop(['Coeff_x', 'Coeff_y'], axis=1, inplace=True)



                                 Feature DF1 DF2
                                0 a 1 1
                                1 b 1 1
                                2 c 1 0
                                3 d 1 0
                                4 y 0 1
                                5 x 0 1



                                I've seen other -- pandas specific-- answers, and I would like to ask what are the advantages of methods like series.str.get_dummies() if you can achieve the same using built-in methods/functions? Is it much faster?
                                Genuinely curious since I'm a newbie myself.



                                (sorry I need more reputation points to leave comments directly under other answers!)







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered 9 hours ago









                                political scientistpolitical scientist

                                658 bronze badges




                                658 bronze badges







                                • 1





                                  Not sure about performance, but you can assign the columns before the merge, then they get brought along df1.assign(DF1=1).merge(....) Just need a .fillna after :D

                                  – ALollz
                                  6 hours ago












                                • 1





                                  Not sure about performance, but you can assign the columns before the merge, then they get brought along df1.assign(DF1=1).merge(....) Just need a .fillna after :D

                                  – ALollz
                                  6 hours ago







                                1




                                1





                                Not sure about performance, but you can assign the columns before the merge, then they get brought along df1.assign(DF1=1).merge(....) Just need a .fillna after :D

                                – ALollz
                                6 hours ago





                                Not sure about performance, but you can assign the columns before the merge, then they get brought along df1.assign(DF1=1).merge(....) Just need a .fillna after :D

                                – ALollz
                                6 hours 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%2f56826251%2fmerging-two-data-frames-into-a-new-one-with-unique-items-marked-with-1-or-0%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

                                Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                                Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                                Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її