Get a count of combinations and its reverse from two columnsHow to get count of two-way combinations from two columns?How to return multiple values from a function?Speed comparison with Project Euler: C vs Python vs Erlang vs HaskellPandas: create two new columns in a dataframe with values calculated from a pre-existing columnDelete column from pandas DataFrame by column name“Large data” work flows using pandasSelect rows from a DataFrame based on values in a column in pandasPandas DataFrame Groupby two columns and get countsGet list from pandas DataFrame column headerspandas create new column based on values from other columnsHow to count unique records by two columns in pandas?

Why is unzipped file smaller than zipped file

Find this Unique UVC Palindrome ( ignoring signs and decimal) from Given Fractional Relationship

Is it normal to "extract a paper" from a master thesis?

How to test if argument is a single space?

Ratings matrix plot

Computing elements of a 1000 x 60 matrix exhausts RAM

If a character has cast the Fly spell on themselves, can they "hand off" to the Levitate spell without interruption?

What is this dime sized black bug with white on the segments near Loveland Colorodao?

Proto-Indo-European (PIE) words with IPA

VHDL: Why is it hard to design a floating point unit in hardware?

Real Analysis: Proof of the equivalent definitions of the derivative.

Does science define life as "beginning at conception"?

How to eliminate gap at the start and at the end of a line when it's drawn along a side of a node's bounding box?

why "American-born", not "America-born"?

What is the winged creature on the back of the Mordenkainen's Tome of Foes book?

Why "strap-on" boosters, and how do other people say it?

Why A=2 and B=1 in the call signs for Spirit and Opportunity?

Keeping the dodos out of the field

Illustrating that universal optimality is stronger than sphere packing

Why do testers need root cause analysis?

How to tease a romance without a cat and mouse chase?

Which values for voltage divider

Is ideal gas incompressible?

Variable does not Exist: CaseTrigger



Get a count of combinations and its reverse from two columns


How to get count of two-way combinations from two columns?How to return multiple values from a function?Speed comparison with Project Euler: C vs Python vs Erlang vs HaskellPandas: create two new columns in a dataframe with values calculated from a pre-existing columnDelete column from pandas DataFrame by column name“Large data” work flows using pandasSelect rows from a DataFrame based on values in a column in pandasPandas DataFrame Groupby two columns and get countsGet list from pandas DataFrame column headerspandas create new column based on values from other columnsHow to count unique records by two columns in pandas?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm trying to get a count of combinations from a pandas dataframe where it views reversed form of the combinations as the same. ie (A/B will be the same as B/A)



Similar to what this user is trying to do, but on python/pandas



How to get count of two-way combinations from two columns?



Thank you for helping!



I've explored crosstabs and grouping the data and it produces a count of the combinations, but it sees the reverse order as a unique combination.



Origin Destination
City 1 City 2
City 2 City 1
City 3 City 4
City 2 City 1


End result will look like



Route Count
City 1 - City 2 3
City 3 - City 4 1


note: order of the route does not matter. It could be City 2 - City 1, as long as it counts it as the same.










share|improve this question




























    0















    I'm trying to get a count of combinations from a pandas dataframe where it views reversed form of the combinations as the same. ie (A/B will be the same as B/A)



    Similar to what this user is trying to do, but on python/pandas



    How to get count of two-way combinations from two columns?



    Thank you for helping!



    I've explored crosstabs and grouping the data and it produces a count of the combinations, but it sees the reverse order as a unique combination.



    Origin Destination
    City 1 City 2
    City 2 City 1
    City 3 City 4
    City 2 City 1


    End result will look like



    Route Count
    City 1 - City 2 3
    City 3 - City 4 1


    note: order of the route does not matter. It could be City 2 - City 1, as long as it counts it as the same.










    share|improve this question
























      0












      0








      0








      I'm trying to get a count of combinations from a pandas dataframe where it views reversed form of the combinations as the same. ie (A/B will be the same as B/A)



      Similar to what this user is trying to do, but on python/pandas



      How to get count of two-way combinations from two columns?



      Thank you for helping!



      I've explored crosstabs and grouping the data and it produces a count of the combinations, but it sees the reverse order as a unique combination.



      Origin Destination
      City 1 City 2
      City 2 City 1
      City 3 City 4
      City 2 City 1


      End result will look like



      Route Count
      City 1 - City 2 3
      City 3 - City 4 1


      note: order of the route does not matter. It could be City 2 - City 1, as long as it counts it as the same.










      share|improve this question














      I'm trying to get a count of combinations from a pandas dataframe where it views reversed form of the combinations as the same. ie (A/B will be the same as B/A)



      Similar to what this user is trying to do, but on python/pandas



      How to get count of two-way combinations from two columns?



      Thank you for helping!



      I've explored crosstabs and grouping the data and it produces a count of the combinations, but it sees the reverse order as a unique combination.



      Origin Destination
      City 1 City 2
      City 2 City 1
      City 3 City 4
      City 2 City 1


      End result will look like



      Route Count
      City 1 - City 2 3
      City 3 - City 4 1


      note: order of the route does not matter. It could be City 2 - City 1, as long as it counts it as the same.







      python pandas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 23 at 21:10









      Tony NguyenTony Nguyen

      1




      1






















          2 Answers
          2






          active

          oldest

          votes


















          1














          You can define a route using np.sort



          import numpy as np
          import pandas as pd

          df['Route'] = [' - '.join(x) for x in np.sort(df.to_numpy(), axis=1)]
          df.groupby('Route').size()

          #Route
          #City 1 - City 2 3
          #City 3 - City 4 1
          #dtype: int64



          You can also construct a new sorted DataFrame, which could be useful:



          df = pd.DataFrame(np.sort(df.to_numpy(), axis=1), index=df.index, columns=df.columns)

          # Origin Destination
          #0 City 1 City 2
          #1 City 1 City 2
          #2 City 3 City 4
          #3 City 1 City 2


          Now you can groupby ['Origin', 'Destintion']






          share|improve this answer























          • Worked like a charm. Thank you Alollz. You are a GENIUS!!!!

            – Tony Nguyen
            Mar 23 at 22:39


















          1














          Check with sort



          df.values.sort()
          df.groupby(list(df)).size()
          Origin Destination
          City1 City2 3
          City3 City4 1
          dtype: int64





          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%2f55318424%2fget-a-count-of-combinations-and-its-reverse-from-two-columns%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 define a route using np.sort



            import numpy as np
            import pandas as pd

            df['Route'] = [' - '.join(x) for x in np.sort(df.to_numpy(), axis=1)]
            df.groupby('Route').size()

            #Route
            #City 1 - City 2 3
            #City 3 - City 4 1
            #dtype: int64



            You can also construct a new sorted DataFrame, which could be useful:



            df = pd.DataFrame(np.sort(df.to_numpy(), axis=1), index=df.index, columns=df.columns)

            # Origin Destination
            #0 City 1 City 2
            #1 City 1 City 2
            #2 City 3 City 4
            #3 City 1 City 2


            Now you can groupby ['Origin', 'Destintion']






            share|improve this answer























            • Worked like a charm. Thank you Alollz. You are a GENIUS!!!!

              – Tony Nguyen
              Mar 23 at 22:39















            1














            You can define a route using np.sort



            import numpy as np
            import pandas as pd

            df['Route'] = [' - '.join(x) for x in np.sort(df.to_numpy(), axis=1)]
            df.groupby('Route').size()

            #Route
            #City 1 - City 2 3
            #City 3 - City 4 1
            #dtype: int64



            You can also construct a new sorted DataFrame, which could be useful:



            df = pd.DataFrame(np.sort(df.to_numpy(), axis=1), index=df.index, columns=df.columns)

            # Origin Destination
            #0 City 1 City 2
            #1 City 1 City 2
            #2 City 3 City 4
            #3 City 1 City 2


            Now you can groupby ['Origin', 'Destintion']






            share|improve this answer























            • Worked like a charm. Thank you Alollz. You are a GENIUS!!!!

              – Tony Nguyen
              Mar 23 at 22:39













            1












            1








            1







            You can define a route using np.sort



            import numpy as np
            import pandas as pd

            df['Route'] = [' - '.join(x) for x in np.sort(df.to_numpy(), axis=1)]
            df.groupby('Route').size()

            #Route
            #City 1 - City 2 3
            #City 3 - City 4 1
            #dtype: int64



            You can also construct a new sorted DataFrame, which could be useful:



            df = pd.DataFrame(np.sort(df.to_numpy(), axis=1), index=df.index, columns=df.columns)

            # Origin Destination
            #0 City 1 City 2
            #1 City 1 City 2
            #2 City 3 City 4
            #3 City 1 City 2


            Now you can groupby ['Origin', 'Destintion']






            share|improve this answer













            You can define a route using np.sort



            import numpy as np
            import pandas as pd

            df['Route'] = [' - '.join(x) for x in np.sort(df.to_numpy(), axis=1)]
            df.groupby('Route').size()

            #Route
            #City 1 - City 2 3
            #City 3 - City 4 1
            #dtype: int64



            You can also construct a new sorted DataFrame, which could be useful:



            df = pd.DataFrame(np.sort(df.to_numpy(), axis=1), index=df.index, columns=df.columns)

            # Origin Destination
            #0 City 1 City 2
            #1 City 1 City 2
            #2 City 3 City 4
            #3 City 1 City 2


            Now you can groupby ['Origin', 'Destintion']







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 23 at 21:23









            ALollzALollz

            18.5k51840




            18.5k51840












            • Worked like a charm. Thank you Alollz. You are a GENIUS!!!!

              – Tony Nguyen
              Mar 23 at 22:39

















            • Worked like a charm. Thank you Alollz. You are a GENIUS!!!!

              – Tony Nguyen
              Mar 23 at 22:39
















            Worked like a charm. Thank you Alollz. You are a GENIUS!!!!

            – Tony Nguyen
            Mar 23 at 22:39





            Worked like a charm. Thank you Alollz. You are a GENIUS!!!!

            – Tony Nguyen
            Mar 23 at 22:39













            1














            Check with sort



            df.values.sort()
            df.groupby(list(df)).size()
            Origin Destination
            City1 City2 3
            City3 City4 1
            dtype: int64





            share|improve this answer



























              1














              Check with sort



              df.values.sort()
              df.groupby(list(df)).size()
              Origin Destination
              City1 City2 3
              City3 City4 1
              dtype: int64





              share|improve this answer

























                1












                1








                1







                Check with sort



                df.values.sort()
                df.groupby(list(df)).size()
                Origin Destination
                City1 City2 3
                City3 City4 1
                dtype: int64





                share|improve this answer













                Check with sort



                df.values.sort()
                df.groupby(list(df)).size()
                Origin Destination
                City1 City2 3
                City3 City4 1
                dtype: int64






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 24 at 0:10









                WeNYoBenWeNYoBen

                135k84474




                135k84474



























                    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%2f55318424%2fget-a-count-of-combinations-and-its-reverse-from-two-columns%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

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현