How to give column names after count and joins?





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







0















I have the following dataframe



import pandas as pd

compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']

df = pd.DataFrame({'company' : compnaies, 'product':products })


I do the following operation:



 df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)])



count product
company
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box


How to name the columns after the above code instead of count and product:



Expected Output:



company     Number   Product List.
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box


Expected Output 2:



Expected Output:



 company        Number   Product List.            uniquecount uniquevalues
Amazon 1 E-comm 1 E-comm
Facebook 1 Social Media 2 Social Media
Google 3 Search, Android, Search 2 Search, Android,
Microsoft 2 OS, X-box, Search 3 OS, X-box,Search









share|improve this question

























  • final_df.reset_index() and rename the columns

    – Madhuri
    Jan 3 at 5:11













  • @Madhuri I tried but it doesn't work I need to rename two columns?

    – panda
    Jan 3 at 5:14


















0















I have the following dataframe



import pandas as pd

compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']

df = pd.DataFrame({'company' : compnaies, 'product':products })


I do the following operation:



 df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)])



count product
company
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box


How to name the columns after the above code instead of count and product:



Expected Output:



company     Number   Product List.
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box


Expected Output 2:



Expected Output:



 company        Number   Product List.            uniquecount uniquevalues
Amazon 1 E-comm 1 E-comm
Facebook 1 Social Media 2 Social Media
Google 3 Search, Android, Search 2 Search, Android,
Microsoft 2 OS, X-box, Search 3 OS, X-box,Search









share|improve this question

























  • final_df.reset_index() and rename the columns

    – Madhuri
    Jan 3 at 5:11













  • @Madhuri I tried but it doesn't work I need to rename two columns?

    – panda
    Jan 3 at 5:14














0












0








0


1






I have the following dataframe



import pandas as pd

compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']

df = pd.DataFrame({'company' : compnaies, 'product':products })


I do the following operation:



 df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)])



count product
company
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box


How to name the columns after the above code instead of count and product:



Expected Output:



company     Number   Product List.
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box


Expected Output 2:



Expected Output:



 company        Number   Product List.            uniquecount uniquevalues
Amazon 1 E-comm 1 E-comm
Facebook 1 Social Media 2 Social Media
Google 3 Search, Android, Search 2 Search, Android,
Microsoft 2 OS, X-box, Search 3 OS, X-box,Search









share|improve this question
















I have the following dataframe



import pandas as pd

compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']

df = pd.DataFrame({'company' : compnaies, 'product':products })


I do the following operation:



 df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)])



count product
company
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box


How to name the columns after the above code instead of count and product:



Expected Output:



company     Number   Product List.
Amazon 1 E-comm
Facebook 1 Social Media
Google 3 Search, Android, Search
Microsoft 2 OS, X-box


Expected Output 2:



Expected Output:



 company        Number   Product List.            uniquecount uniquevalues
Amazon 1 E-comm 1 E-comm
Facebook 1 Social Media 2 Social Media
Google 3 Search, Android, Search 2 Search, Android,
Microsoft 2 OS, X-box, Search 3 OS, X-box,Search






python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 5:36







panda

















asked Jan 3 at 5:08









pandapanda

1608




1608













  • final_df.reset_index() and rename the columns

    – Madhuri
    Jan 3 at 5:11













  • @Madhuri I tried but it doesn't work I need to rename two columns?

    – panda
    Jan 3 at 5:14



















  • final_df.reset_index() and rename the columns

    – Madhuri
    Jan 3 at 5:11













  • @Madhuri I tried but it doesn't work I need to rename two columns?

    – panda
    Jan 3 at 5:14

















final_df.reset_index() and rename the columns

– Madhuri
Jan 3 at 5:11







final_df.reset_index() and rename the columns

– Madhuri
Jan 3 at 5:11















@Madhuri I tried but it doesn't work I need to rename two columns?

– panda
Jan 3 at 5:14





@Madhuri I tried but it doesn't work I need to rename two columns?

– panda
Jan 3 at 5:14












2 Answers
2






active

oldest

votes


















1














import pandas as pd


def remove_dup(string):
temp=string.split(',')
temp=[x.strip() for x in temp]
return ','.join(set(temp))

compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']

df = pd.DataFrame({'company' : compnaies, 'product':products })

new_df=df.groupby('company').product.agg([('Number', 'count'), ('Product list', ', '.join)]).reset_index()

#create uniquevalues
new_df['uniquevalues']=new_df['Product list'].apply(remove_dup)

#create uniquecount
new_df['uniquecount']=new_df['uniquevalues'].str.split(',').str.len()





share|improve this answer


























  • it works How to count the unique values and also print the unique values on the product list can you help?

    – panda
    Jan 3 at 5:18











  • what unique values are you referring to ? , you want to remove duplicate values in df['Product List'] ?

    – pyd
    Jan 3 at 5:21











  • yes I mean that and generate a new column

    – panda
    Jan 3 at 5:22











  • I just added it as Expected output 2

    – panda
    Jan 3 at 5:25






  • 1





    thank u so much, it works

    – panda
    Jan 3 at 5:47





















1














Here is the standard answer:



df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)]).rename(columns={"count":"number","product":"product lists"})


Pandas Online help






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%2f54016652%2fhow-to-give-column-names-after-count-and-joins%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














    import pandas as pd


    def remove_dup(string):
    temp=string.split(',')
    temp=[x.strip() for x in temp]
    return ','.join(set(temp))

    compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
    products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']

    df = pd.DataFrame({'company' : compnaies, 'product':products })

    new_df=df.groupby('company').product.agg([('Number', 'count'), ('Product list', ', '.join)]).reset_index()

    #create uniquevalues
    new_df['uniquevalues']=new_df['Product list'].apply(remove_dup)

    #create uniquecount
    new_df['uniquecount']=new_df['uniquevalues'].str.split(',').str.len()





    share|improve this answer


























    • it works How to count the unique values and also print the unique values on the product list can you help?

      – panda
      Jan 3 at 5:18











    • what unique values are you referring to ? , you want to remove duplicate values in df['Product List'] ?

      – pyd
      Jan 3 at 5:21











    • yes I mean that and generate a new column

      – panda
      Jan 3 at 5:22











    • I just added it as Expected output 2

      – panda
      Jan 3 at 5:25






    • 1





      thank u so much, it works

      – panda
      Jan 3 at 5:47


















    1














    import pandas as pd


    def remove_dup(string):
    temp=string.split(',')
    temp=[x.strip() for x in temp]
    return ','.join(set(temp))

    compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
    products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']

    df = pd.DataFrame({'company' : compnaies, 'product':products })

    new_df=df.groupby('company').product.agg([('Number', 'count'), ('Product list', ', '.join)]).reset_index()

    #create uniquevalues
    new_df['uniquevalues']=new_df['Product list'].apply(remove_dup)

    #create uniquecount
    new_df['uniquecount']=new_df['uniquevalues'].str.split(',').str.len()





    share|improve this answer


























    • it works How to count the unique values and also print the unique values on the product list can you help?

      – panda
      Jan 3 at 5:18











    • what unique values are you referring to ? , you want to remove duplicate values in df['Product List'] ?

      – pyd
      Jan 3 at 5:21











    • yes I mean that and generate a new column

      – panda
      Jan 3 at 5:22











    • I just added it as Expected output 2

      – panda
      Jan 3 at 5:25






    • 1





      thank u so much, it works

      – panda
      Jan 3 at 5:47
















    1












    1








    1







    import pandas as pd


    def remove_dup(string):
    temp=string.split(',')
    temp=[x.strip() for x in temp]
    return ','.join(set(temp))

    compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
    products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']

    df = pd.DataFrame({'company' : compnaies, 'product':products })

    new_df=df.groupby('company').product.agg([('Number', 'count'), ('Product list', ', '.join)]).reset_index()

    #create uniquevalues
    new_df['uniquevalues']=new_df['Product list'].apply(remove_dup)

    #create uniquecount
    new_df['uniquecount']=new_df['uniquevalues'].str.split(',').str.len()





    share|improve this answer















    import pandas as pd


    def remove_dup(string):
    temp=string.split(',')
    temp=[x.strip() for x in temp]
    return ','.join(set(temp))

    compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google','Google']
    products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android','Search']

    df = pd.DataFrame({'company' : compnaies, 'product':products })

    new_df=df.groupby('company').product.agg([('Number', 'count'), ('Product list', ', '.join)]).reset_index()

    #create uniquevalues
    new_df['uniquevalues']=new_df['Product list'].apply(remove_dup)

    #create uniquecount
    new_df['uniquecount']=new_df['uniquevalues'].str.split(',').str.len()






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 3 at 5:43

























    answered Jan 3 at 5:15









    pydpyd

    2,14211229




    2,14211229













    • it works How to count the unique values and also print the unique values on the product list can you help?

      – panda
      Jan 3 at 5:18











    • what unique values are you referring to ? , you want to remove duplicate values in df['Product List'] ?

      – pyd
      Jan 3 at 5:21











    • yes I mean that and generate a new column

      – panda
      Jan 3 at 5:22











    • I just added it as Expected output 2

      – panda
      Jan 3 at 5:25






    • 1





      thank u so much, it works

      – panda
      Jan 3 at 5:47





















    • it works How to count the unique values and also print the unique values on the product list can you help?

      – panda
      Jan 3 at 5:18











    • what unique values are you referring to ? , you want to remove duplicate values in df['Product List'] ?

      – pyd
      Jan 3 at 5:21











    • yes I mean that and generate a new column

      – panda
      Jan 3 at 5:22











    • I just added it as Expected output 2

      – panda
      Jan 3 at 5:25






    • 1





      thank u so much, it works

      – panda
      Jan 3 at 5:47



















    it works How to count the unique values and also print the unique values on the product list can you help?

    – panda
    Jan 3 at 5:18





    it works How to count the unique values and also print the unique values on the product list can you help?

    – panda
    Jan 3 at 5:18













    what unique values are you referring to ? , you want to remove duplicate values in df['Product List'] ?

    – pyd
    Jan 3 at 5:21





    what unique values are you referring to ? , you want to remove duplicate values in df['Product List'] ?

    – pyd
    Jan 3 at 5:21













    yes I mean that and generate a new column

    – panda
    Jan 3 at 5:22





    yes I mean that and generate a new column

    – panda
    Jan 3 at 5:22













    I just added it as Expected output 2

    – panda
    Jan 3 at 5:25





    I just added it as Expected output 2

    – panda
    Jan 3 at 5:25




    1




    1





    thank u so much, it works

    – panda
    Jan 3 at 5:47







    thank u so much, it works

    – panda
    Jan 3 at 5:47















    1














    Here is the standard answer:



    df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)]).rename(columns={"count":"number","product":"product lists"})


    Pandas Online help






    share|improve this answer






























      1














      Here is the standard answer:



      df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)]).rename(columns={"count":"number","product":"product lists"})


      Pandas Online help






      share|improve this answer




























        1












        1








        1







        Here is the standard answer:



        df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)]).rename(columns={"count":"number","product":"product lists"})


        Pandas Online help






        share|improve this answer















        Here is the standard answer:



        df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)]).rename(columns={"count":"number","product":"product lists"})


        Pandas Online help







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 3 at 5:40









        pyd

        2,14211229




        2,14211229










        answered Jan 3 at 5:26









        Yong WangYong Wang

        4613




        4613






























            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%2f54016652%2fhow-to-give-column-names-after-count-and-joins%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

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith