Summarise based on binary values
How can I summarize this dataframe.
OCT NOV DEC JAN Feb Mar Apr May Jun Jul Aug Sep Total Sales
0 0 0 0 0 1 0 0 0 0 0 0 0 4825379.818
1 0 0 0 0 1 0 0 0 0 0 0 0 5522382.297
2 0 0 0 0 1 0 0 0 0 0 0 0 5721183.289
3 0 0 0 0 1 0 0 0 0 0 0 0 5374047.083
4 0 0 0 0 0 1 0 0 0 0 0 0 4917456.971
I want it to be in form
Month Sales
JAN 1235
FEB 5454
.. ..
..
python pandas
add a comment |
How can I summarize this dataframe.
OCT NOV DEC JAN Feb Mar Apr May Jun Jul Aug Sep Total Sales
0 0 0 0 0 1 0 0 0 0 0 0 0 4825379.818
1 0 0 0 0 1 0 0 0 0 0 0 0 5522382.297
2 0 0 0 0 1 0 0 0 0 0 0 0 5721183.289
3 0 0 0 0 1 0 0 0 0 0 0 0 5374047.083
4 0 0 0 0 0 1 0 0 0 0 0 0 4917456.971
I want it to be in form
Month Sales
JAN 1235
FEB 5454
.. ..
..
python pandas
add a comment |
How can I summarize this dataframe.
OCT NOV DEC JAN Feb Mar Apr May Jun Jul Aug Sep Total Sales
0 0 0 0 0 1 0 0 0 0 0 0 0 4825379.818
1 0 0 0 0 1 0 0 0 0 0 0 0 5522382.297
2 0 0 0 0 1 0 0 0 0 0 0 0 5721183.289
3 0 0 0 0 1 0 0 0 0 0 0 0 5374047.083
4 0 0 0 0 0 1 0 0 0 0 0 0 4917456.971
I want it to be in form
Month Sales
JAN 1235
FEB 5454
.. ..
..
python pandas
How can I summarize this dataframe.
OCT NOV DEC JAN Feb Mar Apr May Jun Jul Aug Sep Total Sales
0 0 0 0 0 1 0 0 0 0 0 0 0 4825379.818
1 0 0 0 0 1 0 0 0 0 0 0 0 5522382.297
2 0 0 0 0 1 0 0 0 0 0 0 0 5721183.289
3 0 0 0 0 1 0 0 0 0 0 0 0 5374047.083
4 0 0 0 0 0 1 0 0 0 0 0 0 4917456.971
I want it to be in form
Month Sales
JAN 1235
FEB 5454
.. ..
..
python pandas
python pandas
asked Nov 11 at 10:28
Ayman Alawin
417
417
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can multiple by mul
extracted column by pop
first, then sum
rows for Series
, rename
for uppercase months names, add reindex
for correct order, rename_axis
for nw column name with reset_index
:
cats = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
df1 = (df.mul(df.pop('Total Sales'), axis=0)
.sum()
.rename(str.upper)
.reindex(cats)
.rename_axis('Month')
.reset_index(name='Sales'))
print (df1)
Month Sales
0 JAN 0.000000e+00
1 FEB 2.144299e+07
2 MAR 4.917457e+06
3 APR 0.000000e+00
4 MAY 0.000000e+00
5 JUN 0.000000e+00
6 JUL 0.000000e+00
7 AUG 0.000000e+00
8 SEP 0.000000e+00
9 OCT 0.000000e+00
10 NOV 0.000000e+00
11 DEC 0.000000e+00
1
Downvoter, if there's something wrong with my answer, please let me know, so I can correct it. Thanks.
– jezrael
Nov 11 at 10:34
1
I upvoted the answer
– Ayman Alawin
Nov 11 at 10:38
I need them as separate columns
– Ayman Alawin
Nov 11 at 10:40
1
Give me a sec pelase.
– jezrael
Nov 11 at 10:40
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%2f53247840%2fsummarise-based-on-binary-values%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
You can multiple by mul
extracted column by pop
first, then sum
rows for Series
, rename
for uppercase months names, add reindex
for correct order, rename_axis
for nw column name with reset_index
:
cats = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
df1 = (df.mul(df.pop('Total Sales'), axis=0)
.sum()
.rename(str.upper)
.reindex(cats)
.rename_axis('Month')
.reset_index(name='Sales'))
print (df1)
Month Sales
0 JAN 0.000000e+00
1 FEB 2.144299e+07
2 MAR 4.917457e+06
3 APR 0.000000e+00
4 MAY 0.000000e+00
5 JUN 0.000000e+00
6 JUL 0.000000e+00
7 AUG 0.000000e+00
8 SEP 0.000000e+00
9 OCT 0.000000e+00
10 NOV 0.000000e+00
11 DEC 0.000000e+00
1
Downvoter, if there's something wrong with my answer, please let me know, so I can correct it. Thanks.
– jezrael
Nov 11 at 10:34
1
I upvoted the answer
– Ayman Alawin
Nov 11 at 10:38
I need them as separate columns
– Ayman Alawin
Nov 11 at 10:40
1
Give me a sec pelase.
– jezrael
Nov 11 at 10:40
add a comment |
You can multiple by mul
extracted column by pop
first, then sum
rows for Series
, rename
for uppercase months names, add reindex
for correct order, rename_axis
for nw column name with reset_index
:
cats = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
df1 = (df.mul(df.pop('Total Sales'), axis=0)
.sum()
.rename(str.upper)
.reindex(cats)
.rename_axis('Month')
.reset_index(name='Sales'))
print (df1)
Month Sales
0 JAN 0.000000e+00
1 FEB 2.144299e+07
2 MAR 4.917457e+06
3 APR 0.000000e+00
4 MAY 0.000000e+00
5 JUN 0.000000e+00
6 JUL 0.000000e+00
7 AUG 0.000000e+00
8 SEP 0.000000e+00
9 OCT 0.000000e+00
10 NOV 0.000000e+00
11 DEC 0.000000e+00
1
Downvoter, if there's something wrong with my answer, please let me know, so I can correct it. Thanks.
– jezrael
Nov 11 at 10:34
1
I upvoted the answer
– Ayman Alawin
Nov 11 at 10:38
I need them as separate columns
– Ayman Alawin
Nov 11 at 10:40
1
Give me a sec pelase.
– jezrael
Nov 11 at 10:40
add a comment |
You can multiple by mul
extracted column by pop
first, then sum
rows for Series
, rename
for uppercase months names, add reindex
for correct order, rename_axis
for nw column name with reset_index
:
cats = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
df1 = (df.mul(df.pop('Total Sales'), axis=0)
.sum()
.rename(str.upper)
.reindex(cats)
.rename_axis('Month')
.reset_index(name='Sales'))
print (df1)
Month Sales
0 JAN 0.000000e+00
1 FEB 2.144299e+07
2 MAR 4.917457e+06
3 APR 0.000000e+00
4 MAY 0.000000e+00
5 JUN 0.000000e+00
6 JUL 0.000000e+00
7 AUG 0.000000e+00
8 SEP 0.000000e+00
9 OCT 0.000000e+00
10 NOV 0.000000e+00
11 DEC 0.000000e+00
You can multiple by mul
extracted column by pop
first, then sum
rows for Series
, rename
for uppercase months names, add reindex
for correct order, rename_axis
for nw column name with reset_index
:
cats = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
df1 = (df.mul(df.pop('Total Sales'), axis=0)
.sum()
.rename(str.upper)
.reindex(cats)
.rename_axis('Month')
.reset_index(name='Sales'))
print (df1)
Month Sales
0 JAN 0.000000e+00
1 FEB 2.144299e+07
2 MAR 4.917457e+06
3 APR 0.000000e+00
4 MAY 0.000000e+00
5 JUN 0.000000e+00
6 JUL 0.000000e+00
7 AUG 0.000000e+00
8 SEP 0.000000e+00
9 OCT 0.000000e+00
10 NOV 0.000000e+00
11 DEC 0.000000e+00
edited Nov 11 at 10:41
answered Nov 11 at 10:33
jezrael
319k22258336
319k22258336
1
Downvoter, if there's something wrong with my answer, please let me know, so I can correct it. Thanks.
– jezrael
Nov 11 at 10:34
1
I upvoted the answer
– Ayman Alawin
Nov 11 at 10:38
I need them as separate columns
– Ayman Alawin
Nov 11 at 10:40
1
Give me a sec pelase.
– jezrael
Nov 11 at 10:40
add a comment |
1
Downvoter, if there's something wrong with my answer, please let me know, so I can correct it. Thanks.
– jezrael
Nov 11 at 10:34
1
I upvoted the answer
– Ayman Alawin
Nov 11 at 10:38
I need them as separate columns
– Ayman Alawin
Nov 11 at 10:40
1
Give me a sec pelase.
– jezrael
Nov 11 at 10:40
1
1
Downvoter, if there's something wrong with my answer, please let me know, so I can correct it. Thanks.
– jezrael
Nov 11 at 10:34
Downvoter, if there's something wrong with my answer, please let me know, so I can correct it. Thanks.
– jezrael
Nov 11 at 10:34
1
1
I upvoted the answer
– Ayman Alawin
Nov 11 at 10:38
I upvoted the answer
– Ayman Alawin
Nov 11 at 10:38
I need them as separate columns
– Ayman Alawin
Nov 11 at 10:40
I need them as separate columns
– Ayman Alawin
Nov 11 at 10:40
1
1
Give me a sec pelase.
– jezrael
Nov 11 at 10:40
Give me a sec pelase.
– jezrael
Nov 11 at 10:40
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%2f53247840%2fsummarise-based-on-binary-values%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