Plotting side by side bar graph using two dataframes












0















I have 2 dataframes with the columns brand and count.



Example:



brand | count
------+-------
Gucci | 1234
Chanel| 234444


DF1 has more brands than DF2. I want to create a bar graph where the x axis is all the brands and the y axis is the count. I am not sure how to achieve this so I get a side by side bar graphs for each dataframe grouped by the brands.



  ax = df_pred.plot()
prev_pred.plot(ax=ax)
plt.show()


I tried this code but I cant get it to group by brands. I used sns.barplot to create separate bar graphs but I want to overlay them. I want all the brands in DF1 and so a few of the counts will be 0 for DF2 but that is what I want to compare. Any help is much appreciated.










share|improve this question

























  • You will first want to create a single dataframe with both initial data in it. For use with the pandas plot function you will need a wide form dataframe, for use with seaborn you will need a long form dataframe.

    – ImportanceOfBeingErnest
    Nov 20 '18 at 18:09











  • Ok so if i merge the dataframes (use a left join) how do I make each count column a bar?

    – Py.rookie89
    Nov 20 '18 at 18:23











  • Using joined_df.plot.bar() ?

    – ImportanceOfBeingErnest
    Nov 20 '18 at 18:25











  • If I do that I dont get the names of the brand on the x axis. Can I pass the df rows for the labels?

    – Py.rookie89
    Nov 20 '18 at 18:31











  • Feel free to make your problem reproducible inside the question.

    – ImportanceOfBeingErnest
    Nov 20 '18 at 18:32
















0















I have 2 dataframes with the columns brand and count.



Example:



brand | count
------+-------
Gucci | 1234
Chanel| 234444


DF1 has more brands than DF2. I want to create a bar graph where the x axis is all the brands and the y axis is the count. I am not sure how to achieve this so I get a side by side bar graphs for each dataframe grouped by the brands.



  ax = df_pred.plot()
prev_pred.plot(ax=ax)
plt.show()


I tried this code but I cant get it to group by brands. I used sns.barplot to create separate bar graphs but I want to overlay them. I want all the brands in DF1 and so a few of the counts will be 0 for DF2 but that is what I want to compare. Any help is much appreciated.










share|improve this question

























  • You will first want to create a single dataframe with both initial data in it. For use with the pandas plot function you will need a wide form dataframe, for use with seaborn you will need a long form dataframe.

    – ImportanceOfBeingErnest
    Nov 20 '18 at 18:09











  • Ok so if i merge the dataframes (use a left join) how do I make each count column a bar?

    – Py.rookie89
    Nov 20 '18 at 18:23











  • Using joined_df.plot.bar() ?

    – ImportanceOfBeingErnest
    Nov 20 '18 at 18:25











  • If I do that I dont get the names of the brand on the x axis. Can I pass the df rows for the labels?

    – Py.rookie89
    Nov 20 '18 at 18:31











  • Feel free to make your problem reproducible inside the question.

    – ImportanceOfBeingErnest
    Nov 20 '18 at 18:32














0












0








0








I have 2 dataframes with the columns brand and count.



Example:



brand | count
------+-------
Gucci | 1234
Chanel| 234444


DF1 has more brands than DF2. I want to create a bar graph where the x axis is all the brands and the y axis is the count. I am not sure how to achieve this so I get a side by side bar graphs for each dataframe grouped by the brands.



  ax = df_pred.plot()
prev_pred.plot(ax=ax)
plt.show()


I tried this code but I cant get it to group by brands. I used sns.barplot to create separate bar graphs but I want to overlay them. I want all the brands in DF1 and so a few of the counts will be 0 for DF2 but that is what I want to compare. Any help is much appreciated.










share|improve this question
















I have 2 dataframes with the columns brand and count.



Example:



brand | count
------+-------
Gucci | 1234
Chanel| 234444


DF1 has more brands than DF2. I want to create a bar graph where the x axis is all the brands and the y axis is the count. I am not sure how to achieve this so I get a side by side bar graphs for each dataframe grouped by the brands.



  ax = df_pred.plot()
prev_pred.plot(ax=ax)
plt.show()


I tried this code but I cant get it to group by brands. I used sns.barplot to create separate bar graphs but I want to overlay them. I want all the brands in DF1 and so a few of the counts will be 0 for DF2 but that is what I want to compare. Any help is much appreciated.







python pandas matplotlib seaborn






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 18 '18 at 11:55









j_4321

6,13321530




6,13321530










asked Nov 20 '18 at 18:01









Py.rookie89Py.rookie89

206




206













  • You will first want to create a single dataframe with both initial data in it. For use with the pandas plot function you will need a wide form dataframe, for use with seaborn you will need a long form dataframe.

    – ImportanceOfBeingErnest
    Nov 20 '18 at 18:09











  • Ok so if i merge the dataframes (use a left join) how do I make each count column a bar?

    – Py.rookie89
    Nov 20 '18 at 18:23











  • Using joined_df.plot.bar() ?

    – ImportanceOfBeingErnest
    Nov 20 '18 at 18:25











  • If I do that I dont get the names of the brand on the x axis. Can I pass the df rows for the labels?

    – Py.rookie89
    Nov 20 '18 at 18:31











  • Feel free to make your problem reproducible inside the question.

    – ImportanceOfBeingErnest
    Nov 20 '18 at 18:32



















  • You will first want to create a single dataframe with both initial data in it. For use with the pandas plot function you will need a wide form dataframe, for use with seaborn you will need a long form dataframe.

    – ImportanceOfBeingErnest
    Nov 20 '18 at 18:09











  • Ok so if i merge the dataframes (use a left join) how do I make each count column a bar?

    – Py.rookie89
    Nov 20 '18 at 18:23











  • Using joined_df.plot.bar() ?

    – ImportanceOfBeingErnest
    Nov 20 '18 at 18:25











  • If I do that I dont get the names of the brand on the x axis. Can I pass the df rows for the labels?

    – Py.rookie89
    Nov 20 '18 at 18:31











  • Feel free to make your problem reproducible inside the question.

    – ImportanceOfBeingErnest
    Nov 20 '18 at 18:32

















You will first want to create a single dataframe with both initial data in it. For use with the pandas plot function you will need a wide form dataframe, for use with seaborn you will need a long form dataframe.

– ImportanceOfBeingErnest
Nov 20 '18 at 18:09





You will first want to create a single dataframe with both initial data in it. For use with the pandas plot function you will need a wide form dataframe, for use with seaborn you will need a long form dataframe.

– ImportanceOfBeingErnest
Nov 20 '18 at 18:09













Ok so if i merge the dataframes (use a left join) how do I make each count column a bar?

– Py.rookie89
Nov 20 '18 at 18:23





Ok so if i merge the dataframes (use a left join) how do I make each count column a bar?

– Py.rookie89
Nov 20 '18 at 18:23













Using joined_df.plot.bar() ?

– ImportanceOfBeingErnest
Nov 20 '18 at 18:25





Using joined_df.plot.bar() ?

– ImportanceOfBeingErnest
Nov 20 '18 at 18:25













If I do that I dont get the names of the brand on the x axis. Can I pass the df rows for the labels?

– Py.rookie89
Nov 20 '18 at 18:31





If I do that I dont get the names of the brand on the x axis. Can I pass the df rows for the labels?

– Py.rookie89
Nov 20 '18 at 18:31













Feel free to make your problem reproducible inside the question.

– ImportanceOfBeingErnest
Nov 20 '18 at 18:32





Feel free to make your problem reproducible inside the question.

– ImportanceOfBeingErnest
Nov 20 '18 at 18:32












1 Answer
1






active

oldest

votes


















1














IIUC:



df1 = pd.DataFrame(dict(Brand=[*'GC'], Count=[4, 6]))
df2 = pd.DataFrame(dict(Brand=[*'GCXYZ'], Count=[3, 6, 1, 3, 5]))

pd.concat({
'One': df1.set_index('Brand').Count, 'Two': df2.set_index('Brand').Count
}, axis=1).plot.bar()


enter image description here






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%2f53398903%2fplotting-side-by-side-bar-graph-using-two-dataframes%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    IIUC:



    df1 = pd.DataFrame(dict(Brand=[*'GC'], Count=[4, 6]))
    df2 = pd.DataFrame(dict(Brand=[*'GCXYZ'], Count=[3, 6, 1, 3, 5]))

    pd.concat({
    'One': df1.set_index('Brand').Count, 'Two': df2.set_index('Brand').Count
    }, axis=1).plot.bar()


    enter image description here






    share|improve this answer




























      1














      IIUC:



      df1 = pd.DataFrame(dict(Brand=[*'GC'], Count=[4, 6]))
      df2 = pd.DataFrame(dict(Brand=[*'GCXYZ'], Count=[3, 6, 1, 3, 5]))

      pd.concat({
      'One': df1.set_index('Brand').Count, 'Two': df2.set_index('Brand').Count
      }, axis=1).plot.bar()


      enter image description here






      share|improve this answer


























        1












        1








        1







        IIUC:



        df1 = pd.DataFrame(dict(Brand=[*'GC'], Count=[4, 6]))
        df2 = pd.DataFrame(dict(Brand=[*'GCXYZ'], Count=[3, 6, 1, 3, 5]))

        pd.concat({
        'One': df1.set_index('Brand').Count, 'Two': df2.set_index('Brand').Count
        }, axis=1).plot.bar()


        enter image description here






        share|improve this answer













        IIUC:



        df1 = pd.DataFrame(dict(Brand=[*'GC'], Count=[4, 6]))
        df2 = pd.DataFrame(dict(Brand=[*'GCXYZ'], Count=[3, 6, 1, 3, 5]))

        pd.concat({
        'One': df1.set_index('Brand').Count, 'Two': df2.set_index('Brand').Count
        }, axis=1).plot.bar()


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 18:54









        piRSquaredpiRSquared

        156k22151296




        156k22151296
































            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%2f53398903%2fplotting-side-by-side-bar-graph-using-two-dataframes%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

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?