How to use Pandas to get the count of every combination inclusiveHow to get all possible combinations of a list’s elements?How to get the ASCII value of a character?How to get the current time in PythonHow to get line count cheaply in Python?How do I get the number of elements in a list in Python?How can I count the occurrences of a list item?How to drop rows of Pandas DataFrame whose value in certain columns is NaNHow do I get the row count of a Pandas dataframe?How to iterate over rows in a DataFrame in Pandas?Get list from pandas DataFrame column headersHow to deal with SettingWithCopyWarning in Pandas?

Email Account under attack (really) - anything I can do?

Motorized valve interfering with button?

Can you lasso down a wizard who is using the Levitate spell?

Is it legal to have the "// (c) 2019 John Smith" header in all files when there are hundreds of contributors?

Is there a minimum number of transactions in a block?

Is there really no realistic way for a skeleton monster to move around without magic?

What typically incentivizes a professor to change jobs to a lower ranking university?

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

N.B. ligature in Latex

How is it possible for user's password to be changed after storage was encrypted? (on OS X, Android)

Why is "Reports" in sentence down without "The"

My colleague's body is amazing

Why don't electron-positron collisions release infinite energy?

Circuitry of TV splitters

What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

least quadratic residue under GRH: an EXPLICIT bound

New order #4: World

If Manufacturer spice model and Datasheet give different values which should I use?

I see my dog run

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

Do airline pilots ever risk not hearing communication directed to them specifically, from traffic controllers?

A Journey Through Space and Time

How is the relation "the smallest element is the same" reflexive?



How to use Pandas to get the count of every combination inclusive


How to get all possible combinations of a list’s elements?How to get the ASCII value of a character?How to get the current time in PythonHow to get line count cheaply in Python?How do I get the number of elements in a list in Python?How can I count the occurrences of a list item?How to drop rows of Pandas DataFrame whose value in certain columns is NaNHow do I get the row count of a Pandas dataframe?How to iterate over rows in a DataFrame in Pandas?Get list from pandas DataFrame column headersHow to deal with SettingWithCopyWarning in Pandas?






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








8















I am trying to figure out what combination of clothing customers are buying together. I can figure out the exact combination, but the problem I can't figure out is the count that includes the combination + others.



For example, I have:



Cust_num Item Rev
Cust1 Shirt1 $40
Cust1 Shirt2 $40
Cust1 Shorts1 $40
Cust2 Shirt1 $40
Cust2 Shorts1 $40


This should result in:



Combo Count
Shirt1,Shirt2,Shorts1 1
Shirt1,Shorts1 2


The best I can do is unique combinations:



Combo Count
Shirt1,Shirt2,Shorts1 1
Shirt1,Shorts1 1


I tried:



df = df.pivot(index='Cust_num',columns='Item').sum()
df[df.notnull()] = "x"
df = df.loc[:,"Shirt1":].replace("x", pd.Series(df.columns, df.columns))
col = df.stack().groupby(level=0).apply(','.join)
df2 = pd.DataFrame(col)
df2.groupby([0]).size().reset_index(name='counts')


But that is just the unique counts.










share|improve this question







New contributor




Taylor Smith is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2





    I feel like this is one sort of problem pandas would not be suitable for.

    – coldspeed
    2 hours ago

















8















I am trying to figure out what combination of clothing customers are buying together. I can figure out the exact combination, but the problem I can't figure out is the count that includes the combination + others.



For example, I have:



Cust_num Item Rev
Cust1 Shirt1 $40
Cust1 Shirt2 $40
Cust1 Shorts1 $40
Cust2 Shirt1 $40
Cust2 Shorts1 $40


This should result in:



Combo Count
Shirt1,Shirt2,Shorts1 1
Shirt1,Shorts1 2


The best I can do is unique combinations:



Combo Count
Shirt1,Shirt2,Shorts1 1
Shirt1,Shorts1 1


I tried:



df = df.pivot(index='Cust_num',columns='Item').sum()
df[df.notnull()] = "x"
df = df.loc[:,"Shirt1":].replace("x", pd.Series(df.columns, df.columns))
col = df.stack().groupby(level=0).apply(','.join)
df2 = pd.DataFrame(col)
df2.groupby([0]).size().reset_index(name='counts')


But that is just the unique counts.










share|improve this question







New contributor




Taylor Smith is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2





    I feel like this is one sort of problem pandas would not be suitable for.

    – coldspeed
    2 hours ago













8












8








8








I am trying to figure out what combination of clothing customers are buying together. I can figure out the exact combination, but the problem I can't figure out is the count that includes the combination + others.



For example, I have:



Cust_num Item Rev
Cust1 Shirt1 $40
Cust1 Shirt2 $40
Cust1 Shorts1 $40
Cust2 Shirt1 $40
Cust2 Shorts1 $40


This should result in:



Combo Count
Shirt1,Shirt2,Shorts1 1
Shirt1,Shorts1 2


The best I can do is unique combinations:



Combo Count
Shirt1,Shirt2,Shorts1 1
Shirt1,Shorts1 1


I tried:



df = df.pivot(index='Cust_num',columns='Item').sum()
df[df.notnull()] = "x"
df = df.loc[:,"Shirt1":].replace("x", pd.Series(df.columns, df.columns))
col = df.stack().groupby(level=0).apply(','.join)
df2 = pd.DataFrame(col)
df2.groupby([0]).size().reset_index(name='counts')


But that is just the unique counts.










share|improve this question







New contributor




Taylor Smith is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I am trying to figure out what combination of clothing customers are buying together. I can figure out the exact combination, but the problem I can't figure out is the count that includes the combination + others.



For example, I have:



Cust_num Item Rev
Cust1 Shirt1 $40
Cust1 Shirt2 $40
Cust1 Shorts1 $40
Cust2 Shirt1 $40
Cust2 Shorts1 $40


This should result in:



Combo Count
Shirt1,Shirt2,Shorts1 1
Shirt1,Shorts1 2


The best I can do is unique combinations:



Combo Count
Shirt1,Shirt2,Shorts1 1
Shirt1,Shorts1 1


I tried:



df = df.pivot(index='Cust_num',columns='Item').sum()
df[df.notnull()] = "x"
df = df.loc[:,"Shirt1":].replace("x", pd.Series(df.columns, df.columns))
col = df.stack().groupby(level=0).apply(','.join)
df2 = pd.DataFrame(col)
df2.groupby([0]).size().reset_index(name='counts')


But that is just the unique counts.







python pandas






share|improve this question







New contributor




Taylor Smith is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Taylor Smith is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Taylor Smith is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









Taylor SmithTaylor Smith

442




442




New contributor




Taylor Smith is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Taylor Smith is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Taylor Smith is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 2





    I feel like this is one sort of problem pandas would not be suitable for.

    – coldspeed
    2 hours ago












  • 2





    I feel like this is one sort of problem pandas would not be suitable for.

    – coldspeed
    2 hours ago







2




2





I feel like this is one sort of problem pandas would not be suitable for.

– coldspeed
2 hours ago





I feel like this is one sort of problem pandas would not be suitable for.

– coldspeed
2 hours ago












4 Answers
4






active

oldest

votes


















4














Using pandas.DataFrame.groupby:



grouped_item = df.groupby('Cust_num')['Item']
subsets = grouped_item.apply(lambda x: set(x)).tolist()
Count = [sum(s2.issubset(s1) for s1 in subsets) for s2 in subsets]
combo = grouped_item.apply(lambda x:','.join(x))
combo = combo.reset_index()
combo['Count']=Count


Output:



 Cust_num Item Count
0 Cust1 Shirt1,Shirt2,Shorts1 1
1 Cust2 Shirt1,Shorts1 2





share|improve this answer






























    2














    Late answer, but you can use:



    df = df.groupby(['Cust_num'], as_index=False).agg(','.join).drop(columns=['Rev']).set_index(['Item']).rename_axis("combo").rename(columns="Cust_num": "Count")
    df['Count'] = df['Count'].str.replace(r'Cust','')



    combo Count 
    Shirt1,Shirt2,Shorts1 1
    Shirt1,Shorts1 2





    share|improve this answer
































      1














      I think you need to create a combination of items first.



      How to get all possible combinations of a list’s elements?



      I used the function from Dan H's answer.



      from itertools import chain, combinations
      def all_subsets(ss):
      return chain(*map(lambda x: combinations(ss, x), range(0, len(ss)+1)))


      Then get the unique items.



      uq_items = df.Item.unique()

      list(all_subsets(uq_items))

      [(),
      ('Shirt1',),
      ('Shirt2',),
      ('Shorts1',),
      ('Shirt1', 'Shirt2'),
      ('Shirt1', 'Shorts1'),
      ('Shirt2', 'Shorts1'),
      ('Shirt1', 'Shirt2', 'Shorts1')]


      And use groupby each customer to get their items combination.



      ls = []

      for _, d in df.groupby('Cust_num', group_keys=False):
      # Get all possible subset of items
      pi = np.array(list(all_subsets(d.Item)))

      # Fliter only > 1
      ls.append(pi[[len(l) > 1 for l in pi]])


      Then convert to Series and use value_counts().



      pd.Series(np.concatenate(ls)).value_counts()

      (Shirt1, Shorts1) 2
      (Shirt2, Shorts1) 1
      (Shirt1, Shirt2, Shorts1) 1
      (Shirt1, Shirt2) 1





      share|improve this answer






























        -1














        My version which I believe is easier to understand



        new_df = df.groupby("Cust_num").agg(lambda x: ''.join(x.unique()))

        new_df ['count'] = range(1, len(new_df ) + 1)


        Output:



         Item Rev count
        <lambda> <lambda>
        Cust_num
        Cust1 Shirt1 Shirt2 Shorts1 $40 1
        Cust2 Shirt1 Shorts1 $40 2


        Since you do not need the Rev column, you can drop it:



        new_df = new_df = new_df.drop(columns=["Rev"]).reset_index()

        new_df


        Output:



         Cust_num Item count
        <lambda>
        0 Cust1 Shirt1 Shirt2 Shorts1 1
        1 Cust2 Shirt1 Shorts1 2





        share|improve this answer

























        • How is the count in your answer the count of inclusive combination of df['Item']? Making new column with range is not an answer.

          – Chris
          1 hour 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
        );



        );






        Taylor Smith is a new contributor. Be nice, and check out our Code of Conduct.









        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55565916%2fhow-to-use-pandas-to-get-the-count-of-every-combination-inclusive%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









        4














        Using pandas.DataFrame.groupby:



        grouped_item = df.groupby('Cust_num')['Item']
        subsets = grouped_item.apply(lambda x: set(x)).tolist()
        Count = [sum(s2.issubset(s1) for s1 in subsets) for s2 in subsets]
        combo = grouped_item.apply(lambda x:','.join(x))
        combo = combo.reset_index()
        combo['Count']=Count


        Output:



         Cust_num Item Count
        0 Cust1 Shirt1,Shirt2,Shorts1 1
        1 Cust2 Shirt1,Shorts1 2





        share|improve this answer



























          4














          Using pandas.DataFrame.groupby:



          grouped_item = df.groupby('Cust_num')['Item']
          subsets = grouped_item.apply(lambda x: set(x)).tolist()
          Count = [sum(s2.issubset(s1) for s1 in subsets) for s2 in subsets]
          combo = grouped_item.apply(lambda x:','.join(x))
          combo = combo.reset_index()
          combo['Count']=Count


          Output:



           Cust_num Item Count
          0 Cust1 Shirt1,Shirt2,Shorts1 1
          1 Cust2 Shirt1,Shorts1 2





          share|improve this answer

























            4












            4








            4







            Using pandas.DataFrame.groupby:



            grouped_item = df.groupby('Cust_num')['Item']
            subsets = grouped_item.apply(lambda x: set(x)).tolist()
            Count = [sum(s2.issubset(s1) for s1 in subsets) for s2 in subsets]
            combo = grouped_item.apply(lambda x:','.join(x))
            combo = combo.reset_index()
            combo['Count']=Count


            Output:



             Cust_num Item Count
            0 Cust1 Shirt1,Shirt2,Shorts1 1
            1 Cust2 Shirt1,Shorts1 2





            share|improve this answer













            Using pandas.DataFrame.groupby:



            grouped_item = df.groupby('Cust_num')['Item']
            subsets = grouped_item.apply(lambda x: set(x)).tolist()
            Count = [sum(s2.issubset(s1) for s1 in subsets) for s2 in subsets]
            combo = grouped_item.apply(lambda x:','.join(x))
            combo = combo.reset_index()
            combo['Count']=Count


            Output:



             Cust_num Item Count
            0 Cust1 Shirt1,Shirt2,Shorts1 1
            1 Cust2 Shirt1,Shorts1 2






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 hours ago









            ChrisChris

            3,710422




            3,710422























                2














                Late answer, but you can use:



                df = df.groupby(['Cust_num'], as_index=False).agg(','.join).drop(columns=['Rev']).set_index(['Item']).rename_axis("combo").rename(columns="Cust_num": "Count")
                df['Count'] = df['Count'].str.replace(r'Cust','')



                combo Count 
                Shirt1,Shirt2,Shorts1 1
                Shirt1,Shorts1 2





                share|improve this answer





























                  2














                  Late answer, but you can use:



                  df = df.groupby(['Cust_num'], as_index=False).agg(','.join).drop(columns=['Rev']).set_index(['Item']).rename_axis("combo").rename(columns="Cust_num": "Count")
                  df['Count'] = df['Count'].str.replace(r'Cust','')



                  combo Count 
                  Shirt1,Shirt2,Shorts1 1
                  Shirt1,Shorts1 2





                  share|improve this answer



























                    2












                    2








                    2







                    Late answer, but you can use:



                    df = df.groupby(['Cust_num'], as_index=False).agg(','.join).drop(columns=['Rev']).set_index(['Item']).rename_axis("combo").rename(columns="Cust_num": "Count")
                    df['Count'] = df['Count'].str.replace(r'Cust','')



                    combo Count 
                    Shirt1,Shirt2,Shorts1 1
                    Shirt1,Shorts1 2





                    share|improve this answer















                    Late answer, but you can use:



                    df = df.groupby(['Cust_num'], as_index=False).agg(','.join).drop(columns=['Rev']).set_index(['Item']).rename_axis("combo").rename(columns="Cust_num": "Count")
                    df['Count'] = df['Count'].str.replace(r'Cust','')



                    combo Count 
                    Shirt1,Shirt2,Shorts1 1
                    Shirt1,Shorts1 2






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 1 hour ago

























                    answered 1 hour ago









                    Pedro LobitoPedro Lobito

                    50.5k16138172




                    50.5k16138172





















                        1














                        I think you need to create a combination of items first.



                        How to get all possible combinations of a list’s elements?



                        I used the function from Dan H's answer.



                        from itertools import chain, combinations
                        def all_subsets(ss):
                        return chain(*map(lambda x: combinations(ss, x), range(0, len(ss)+1)))


                        Then get the unique items.



                        uq_items = df.Item.unique()

                        list(all_subsets(uq_items))

                        [(),
                        ('Shirt1',),
                        ('Shirt2',),
                        ('Shorts1',),
                        ('Shirt1', 'Shirt2'),
                        ('Shirt1', 'Shorts1'),
                        ('Shirt2', 'Shorts1'),
                        ('Shirt1', 'Shirt2', 'Shorts1')]


                        And use groupby each customer to get their items combination.



                        ls = []

                        for _, d in df.groupby('Cust_num', group_keys=False):
                        # Get all possible subset of items
                        pi = np.array(list(all_subsets(d.Item)))

                        # Fliter only > 1
                        ls.append(pi[[len(l) > 1 for l in pi]])


                        Then convert to Series and use value_counts().



                        pd.Series(np.concatenate(ls)).value_counts()

                        (Shirt1, Shorts1) 2
                        (Shirt2, Shorts1) 1
                        (Shirt1, Shirt2, Shorts1) 1
                        (Shirt1, Shirt2) 1





                        share|improve this answer



























                          1














                          I think you need to create a combination of items first.



                          How to get all possible combinations of a list’s elements?



                          I used the function from Dan H's answer.



                          from itertools import chain, combinations
                          def all_subsets(ss):
                          return chain(*map(lambda x: combinations(ss, x), range(0, len(ss)+1)))


                          Then get the unique items.



                          uq_items = df.Item.unique()

                          list(all_subsets(uq_items))

                          [(),
                          ('Shirt1',),
                          ('Shirt2',),
                          ('Shorts1',),
                          ('Shirt1', 'Shirt2'),
                          ('Shirt1', 'Shorts1'),
                          ('Shirt2', 'Shorts1'),
                          ('Shirt1', 'Shirt2', 'Shorts1')]


                          And use groupby each customer to get their items combination.



                          ls = []

                          for _, d in df.groupby('Cust_num', group_keys=False):
                          # Get all possible subset of items
                          pi = np.array(list(all_subsets(d.Item)))

                          # Fliter only > 1
                          ls.append(pi[[len(l) > 1 for l in pi]])


                          Then convert to Series and use value_counts().



                          pd.Series(np.concatenate(ls)).value_counts()

                          (Shirt1, Shorts1) 2
                          (Shirt2, Shorts1) 1
                          (Shirt1, Shirt2, Shorts1) 1
                          (Shirt1, Shirt2) 1





                          share|improve this answer

























                            1












                            1








                            1







                            I think you need to create a combination of items first.



                            How to get all possible combinations of a list’s elements?



                            I used the function from Dan H's answer.



                            from itertools import chain, combinations
                            def all_subsets(ss):
                            return chain(*map(lambda x: combinations(ss, x), range(0, len(ss)+1)))


                            Then get the unique items.



                            uq_items = df.Item.unique()

                            list(all_subsets(uq_items))

                            [(),
                            ('Shirt1',),
                            ('Shirt2',),
                            ('Shorts1',),
                            ('Shirt1', 'Shirt2'),
                            ('Shirt1', 'Shorts1'),
                            ('Shirt2', 'Shorts1'),
                            ('Shirt1', 'Shirt2', 'Shorts1')]


                            And use groupby each customer to get their items combination.



                            ls = []

                            for _, d in df.groupby('Cust_num', group_keys=False):
                            # Get all possible subset of items
                            pi = np.array(list(all_subsets(d.Item)))

                            # Fliter only > 1
                            ls.append(pi[[len(l) > 1 for l in pi]])


                            Then convert to Series and use value_counts().



                            pd.Series(np.concatenate(ls)).value_counts()

                            (Shirt1, Shorts1) 2
                            (Shirt2, Shorts1) 1
                            (Shirt1, Shirt2, Shorts1) 1
                            (Shirt1, Shirt2) 1





                            share|improve this answer













                            I think you need to create a combination of items first.



                            How to get all possible combinations of a list’s elements?



                            I used the function from Dan H's answer.



                            from itertools import chain, combinations
                            def all_subsets(ss):
                            return chain(*map(lambda x: combinations(ss, x), range(0, len(ss)+1)))


                            Then get the unique items.



                            uq_items = df.Item.unique()

                            list(all_subsets(uq_items))

                            [(),
                            ('Shirt1',),
                            ('Shirt2',),
                            ('Shorts1',),
                            ('Shirt1', 'Shirt2'),
                            ('Shirt1', 'Shorts1'),
                            ('Shirt2', 'Shorts1'),
                            ('Shirt1', 'Shirt2', 'Shorts1')]


                            And use groupby each customer to get their items combination.



                            ls = []

                            for _, d in df.groupby('Cust_num', group_keys=False):
                            # Get all possible subset of items
                            pi = np.array(list(all_subsets(d.Item)))

                            # Fliter only > 1
                            ls.append(pi[[len(l) > 1 for l in pi]])


                            Then convert to Series and use value_counts().



                            pd.Series(np.concatenate(ls)).value_counts()

                            (Shirt1, Shorts1) 2
                            (Shirt2, Shorts1) 1
                            (Shirt1, Shirt2, Shorts1) 1
                            (Shirt1, Shirt2) 1






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 1 hour ago









                            ResidentSleeperResidentSleeper

                            36210




                            36210





















                                -1














                                My version which I believe is easier to understand



                                new_df = df.groupby("Cust_num").agg(lambda x: ''.join(x.unique()))

                                new_df ['count'] = range(1, len(new_df ) + 1)


                                Output:



                                 Item Rev count
                                <lambda> <lambda>
                                Cust_num
                                Cust1 Shirt1 Shirt2 Shorts1 $40 1
                                Cust2 Shirt1 Shorts1 $40 2


                                Since you do not need the Rev column, you can drop it:



                                new_df = new_df = new_df.drop(columns=["Rev"]).reset_index()

                                new_df


                                Output:



                                 Cust_num Item count
                                <lambda>
                                0 Cust1 Shirt1 Shirt2 Shorts1 1
                                1 Cust2 Shirt1 Shorts1 2





                                share|improve this answer

























                                • How is the count in your answer the count of inclusive combination of df['Item']? Making new column with range is not an answer.

                                  – Chris
                                  1 hour ago
















                                -1














                                My version which I believe is easier to understand



                                new_df = df.groupby("Cust_num").agg(lambda x: ''.join(x.unique()))

                                new_df ['count'] = range(1, len(new_df ) + 1)


                                Output:



                                 Item Rev count
                                <lambda> <lambda>
                                Cust_num
                                Cust1 Shirt1 Shirt2 Shorts1 $40 1
                                Cust2 Shirt1 Shorts1 $40 2


                                Since you do not need the Rev column, you can drop it:



                                new_df = new_df = new_df.drop(columns=["Rev"]).reset_index()

                                new_df


                                Output:



                                 Cust_num Item count
                                <lambda>
                                0 Cust1 Shirt1 Shirt2 Shorts1 1
                                1 Cust2 Shirt1 Shorts1 2





                                share|improve this answer

























                                • How is the count in your answer the count of inclusive combination of df['Item']? Making new column with range is not an answer.

                                  – Chris
                                  1 hour ago














                                -1












                                -1








                                -1







                                My version which I believe is easier to understand



                                new_df = df.groupby("Cust_num").agg(lambda x: ''.join(x.unique()))

                                new_df ['count'] = range(1, len(new_df ) + 1)


                                Output:



                                 Item Rev count
                                <lambda> <lambda>
                                Cust_num
                                Cust1 Shirt1 Shirt2 Shorts1 $40 1
                                Cust2 Shirt1 Shorts1 $40 2


                                Since you do not need the Rev column, you can drop it:



                                new_df = new_df = new_df.drop(columns=["Rev"]).reset_index()

                                new_df


                                Output:



                                 Cust_num Item count
                                <lambda>
                                0 Cust1 Shirt1 Shirt2 Shorts1 1
                                1 Cust2 Shirt1 Shorts1 2





                                share|improve this answer















                                My version which I believe is easier to understand



                                new_df = df.groupby("Cust_num").agg(lambda x: ''.join(x.unique()))

                                new_df ['count'] = range(1, len(new_df ) + 1)


                                Output:



                                 Item Rev count
                                <lambda> <lambda>
                                Cust_num
                                Cust1 Shirt1 Shirt2 Shorts1 $40 1
                                Cust2 Shirt1 Shorts1 $40 2


                                Since you do not need the Rev column, you can drop it:



                                new_df = new_df = new_df.drop(columns=["Rev"]).reset_index()

                                new_df


                                Output:



                                 Cust_num Item count
                                <lambda>
                                0 Cust1 Shirt1 Shirt2 Shorts1 1
                                1 Cust2 Shirt1 Shorts1 2






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited 1 hour ago

























                                answered 1 hour ago









                                Lee MtotiLee Mtoti

                                13110




                                13110












                                • How is the count in your answer the count of inclusive combination of df['Item']? Making new column with range is not an answer.

                                  – Chris
                                  1 hour ago


















                                • How is the count in your answer the count of inclusive combination of df['Item']? Making new column with range is not an answer.

                                  – Chris
                                  1 hour ago

















                                How is the count in your answer the count of inclusive combination of df['Item']? Making new column with range is not an answer.

                                – Chris
                                1 hour ago






                                How is the count in your answer the count of inclusive combination of df['Item']? Making new column with range is not an answer.

                                – Chris
                                1 hour ago











                                Taylor Smith is a new contributor. Be nice, and check out our Code of Conduct.









                                draft saved

                                draft discarded


















                                Taylor Smith is a new contributor. Be nice, and check out our Code of Conduct.












                                Taylor Smith is a new contributor. Be nice, and check out our Code of Conduct.











                                Taylor Smith is a new contributor. Be nice, and check out our Code of Conduct.














                                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%2f55565916%2fhow-to-use-pandas-to-get-the-count-of-every-combination-inclusive%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

                                Magento 2 duplicate PHPSESSID cookie when using session_start() in custom php scriptMagento 2: User cant logged in into to account page, no error showing!Magento duplicate on subdomainGrabbing storeview from cookie (after using language selector)How do I run php custom script on magento2Magento 2: Include PHP script in headerSession lock after using Cm_RedisSessionscript php to update stockMagento set cookie popupMagento 2 session id cookie - where to find it?How to import Configurable product from csv with custom attributes using php scriptMagento 2 run custom PHP script

                                Can not update quote_id field of “quote_item” table magento 2Magento 2.1 - We can't remove the item. (Shopping Cart doesnt allow us to remove items before becomes empty)Add value for custom quote item attribute using REST apiREST API endpoint v1/carts/cartId/items always returns error messageCorrect way to save entries to databaseHow to remove all associated quote objects of a customer completelyMagento 2 - Save value from custom input field to quote_itemGet quote_item data using quote id and product id filter in Magento 2How to set additional data to quote_item table from controller in Magento 2?What is the purpose of additional_data column in quote_item table in magento2Set Custom Price to Quote item magento2 from controller

                                How to solve knockout JS error in Magento 2 Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?(Magento2) knockout.js:3012 Uncaught ReferenceError: Unable to process bindingUnable to process binding Knockout.js magento 2Cannot read property `scopeLabel` of undefined on Product Detail PageCan't get Customer Data on frontend in Magento 2Magento2 Order Summary - unable to process bindingKO templates are not loading in Magento 2.1 applicationgetting knockout js error magento 2Product grid not load -— Unable to process binding Knockout.js magento 2Product form not loaded in magento2Uncaught ReferenceError: Unable to process binding “if: function()return (isShowLegend()) ” magento 2