Plotting side by side bar graph using two dataframes
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
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 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 at 18:23
Usingjoined_df.plot.bar()
?
– ImportanceOfBeingErnest
Nov 20 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 at 18:31
Feel free to make your problem reproducible inside the question.
– ImportanceOfBeingErnest
Nov 20 at 18:32
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
python pandas matplotlib seaborn
edited yesterday
j_4321
5,92321530
5,92321530
asked Nov 20 at 18:01
Py.rookie89
245
245
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 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 at 18:23
Usingjoined_df.plot.bar()
?
– ImportanceOfBeingErnest
Nov 20 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 at 18:31
Feel free to make your problem reproducible inside the question.
– ImportanceOfBeingErnest
Nov 20 at 18:32
add a comment |
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 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 at 18:23
Usingjoined_df.plot.bar()
?
– ImportanceOfBeingErnest
Nov 20 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 at 18:31
Feel free to make your problem reproducible inside the question.
– ImportanceOfBeingErnest
Nov 20 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 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 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 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 at 18:23
Using
joined_df.plot.bar()
?– ImportanceOfBeingErnest
Nov 20 at 18:25
Using
joined_df.plot.bar()
?– ImportanceOfBeingErnest
Nov 20 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 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 at 18:31
Feel free to make your problem reproducible inside the question.
– ImportanceOfBeingErnest
Nov 20 at 18:32
Feel free to make your problem reproducible inside the question.
– ImportanceOfBeingErnest
Nov 20 at 18:32
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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()
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
up vote
0
down vote
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()
add a comment |
up vote
0
down vote
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()
add a comment |
up vote
0
down vote
up vote
0
down vote
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()
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()
answered Nov 20 at 18:54
piRSquared
151k22141283
151k22141283
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 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 at 18:23
Using
joined_df.plot.bar()
?– ImportanceOfBeingErnest
Nov 20 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 at 18:31
Feel free to make your problem reproducible inside the question.
– ImportanceOfBeingErnest
Nov 20 at 18:32