How to create a df that gets sum of columns based on a groupby column? The Next CEO of Stack Overflow2019 Community Moderator ElectionCreate a new column based on two columns from two different dataframesHow to sum values grouped by two columns in pandasCreate new data frames from existing data frame based on unique column valuesLow silhouette coefficientShould I use pandas get_dummies and create additional columns or use my own encoding code that keeps 1 column?How to calculate Cumulative Sum with Groupby in Python?How to create a new column based on two other columns in Pandas?how to update column in data frame based on conditionPandas merge column duplicate and sum valueMultiple filtering pandas columns based on values in another column
Can I hook these wires up to find the connection to a dead outlet?
Could a dragon use hot air to help it take off?
How badly should I try to prevent a user from XSSing themselves?
Prodigo = pro + ago?
My singleton can be called multiple times
Can I cast Thunderwave and be at the center of its bottom face, but not be affected by it?
How dangerous is XSS
Is it possible to create a QR code using text?
A hang glider, sudden unexpected lift to 25,000 feet altitude, what could do this?
Oldie but Goldie
How exploitable/balanced is this homebrew spell: Spell Permanency?
Read/write a pipe-delimited file line by line with some simple text manipulation
Is a distribution that is normal, but highly skewed, considered Gaussian?
Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?
pgfplots: How to draw a tangent graph below two others?
Can you teleport closer to a creature you are Frightened of?
What does this strange code stamp on my passport mean?
How to travel Japan while expressing milk?
Was the Stack Exchange "Happy April Fools" page fitting with the 90s code?
Can this transistor (2N2222) take 6 V on emitter-base? Am I reading the datasheet incorrectly?
Is it possible to get a referendum by a court decision?
How to detect int overflow in C
Why did the Drakh emissary look so blurred in S04:E11 "Lines of Communication"?
Find the majority element, which appears more than half the time
How to create a df that gets sum of columns based on a groupby column?
The Next CEO of Stack Overflow2019 Community Moderator ElectionCreate a new column based on two columns from two different dataframesHow to sum values grouped by two columns in pandasCreate new data frames from existing data frame based on unique column valuesLow silhouette coefficientShould I use pandas get_dummies and create additional columns or use my own encoding code that keeps 1 column?How to calculate Cumulative Sum with Groupby in Python?How to create a new column based on two other columns in Pandas?how to update column in data frame based on conditionPandas merge column duplicate and sum valueMultiple filtering pandas columns based on values in another column
$begingroup$
country year gender measure value0 ... value12
A 2000 1 vaccinated_at_month 2 ... 1
B 2000 1 vaccinated_at_month 13 ... 12
A 2000 0 vaccinated_at_month 4 ... 3
A 2000 9 vaccinated_at_month 5 ... 4
B 2000 0 walked_at_month 3 ... 13
C 2001 1 vaccinated_at_month 6 ... 5
C 2001 0 vaccinated_at_month 3 ... 2
I want to be able to remove the gender column and collapse all values into sums based on the previous categorical columns.
I have tried
df_new = df.groupby(['country', 'year', 'gender', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())
However, df_new still preserves the gender column. I am trying to get this outcome:
country year measure value0 ... value12
A 2000 vaccinated_at_month 11 (=2+4+5) ... 8 (=1+3+4)
B 2000 vaccinated_at_month 13 ... 12
B 2000 walked_at_month 3 ... 13
C 2001 vaccinated_at_month 9 (=6+3) ... 7 (=5+2)
C 2001 vaccinated_at_month 3 ... 2
python pandas dataframe
$endgroup$
add a comment |
$begingroup$
country year gender measure value0 ... value12
A 2000 1 vaccinated_at_month 2 ... 1
B 2000 1 vaccinated_at_month 13 ... 12
A 2000 0 vaccinated_at_month 4 ... 3
A 2000 9 vaccinated_at_month 5 ... 4
B 2000 0 walked_at_month 3 ... 13
C 2001 1 vaccinated_at_month 6 ... 5
C 2001 0 vaccinated_at_month 3 ... 2
I want to be able to remove the gender column and collapse all values into sums based on the previous categorical columns.
I have tried
df_new = df.groupby(['country', 'year', 'gender', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())
However, df_new still preserves the gender column. I am trying to get this outcome:
country year measure value0 ... value12
A 2000 vaccinated_at_month 11 (=2+4+5) ... 8 (=1+3+4)
B 2000 vaccinated_at_month 13 ... 12
B 2000 walked_at_month 3 ... 13
C 2001 vaccinated_at_month 9 (=6+3) ... 7 (=5+2)
C 2001 vaccinated_at_month 3 ... 2
python pandas dataframe
$endgroup$
add a comment |
$begingroup$
country year gender measure value0 ... value12
A 2000 1 vaccinated_at_month 2 ... 1
B 2000 1 vaccinated_at_month 13 ... 12
A 2000 0 vaccinated_at_month 4 ... 3
A 2000 9 vaccinated_at_month 5 ... 4
B 2000 0 walked_at_month 3 ... 13
C 2001 1 vaccinated_at_month 6 ... 5
C 2001 0 vaccinated_at_month 3 ... 2
I want to be able to remove the gender column and collapse all values into sums based on the previous categorical columns.
I have tried
df_new = df.groupby(['country', 'year', 'gender', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())
However, df_new still preserves the gender column. I am trying to get this outcome:
country year measure value0 ... value12
A 2000 vaccinated_at_month 11 (=2+4+5) ... 8 (=1+3+4)
B 2000 vaccinated_at_month 13 ... 12
B 2000 walked_at_month 3 ... 13
C 2001 vaccinated_at_month 9 (=6+3) ... 7 (=5+2)
C 2001 vaccinated_at_month 3 ... 2
python pandas dataframe
$endgroup$
country year gender measure value0 ... value12
A 2000 1 vaccinated_at_month 2 ... 1
B 2000 1 vaccinated_at_month 13 ... 12
A 2000 0 vaccinated_at_month 4 ... 3
A 2000 9 vaccinated_at_month 5 ... 4
B 2000 0 walked_at_month 3 ... 13
C 2001 1 vaccinated_at_month 6 ... 5
C 2001 0 vaccinated_at_month 3 ... 2
I want to be able to remove the gender column and collapse all values into sums based on the previous categorical columns.
I have tried
df_new = df.groupby(['country', 'year', 'gender', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())
However, df_new still preserves the gender column. I am trying to get this outcome:
country year measure value0 ... value12
A 2000 vaccinated_at_month 11 (=2+4+5) ... 8 (=1+3+4)
B 2000 vaccinated_at_month 13 ... 12
B 2000 walked_at_month 3 ... 13
C 2001 vaccinated_at_month 9 (=6+3) ... 7 (=5+2)
C 2001 vaccinated_at_month 3 ... 2
python pandas dataframe
python pandas dataframe
edited Mar 25 at 18:26
Simon Larsson
588112
588112
asked Mar 25 at 15:32
user70182user70182
41
41
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
This question is more appropriate at Stackoverflow since it is more of a programming question.
However, I think you almost got it working. Just remove gender from your groupby:
df_new = df.groupby(['country', 'year', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())
$endgroup$
add a comment |
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "557"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdatascience.stackexchange.com%2fquestions%2f47953%2fhow-to-create-a-df-that-gets-sum-of-columns-based-on-a-groupby-column%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
$begingroup$
This question is more appropriate at Stackoverflow since it is more of a programming question.
However, I think you almost got it working. Just remove gender from your groupby:
df_new = df.groupby(['country', 'year', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())
$endgroup$
add a comment |
$begingroup$
This question is more appropriate at Stackoverflow since it is more of a programming question.
However, I think you almost got it working. Just remove gender from your groupby:
df_new = df.groupby(['country', 'year', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())
$endgroup$
add a comment |
$begingroup$
This question is more appropriate at Stackoverflow since it is more of a programming question.
However, I think you almost got it working. Just remove gender from your groupby:
df_new = df.groupby(['country', 'year', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())
$endgroup$
This question is more appropriate at Stackoverflow since it is more of a programming question.
However, I think you almost got it working. Just remove gender from your groupby:
df_new = df.groupby(['country', 'year', 'measure'])['value0', ... 'value12'].apply(lambda x : x.astype(float).sum())
answered Mar 25 at 16:46
Simon LarssonSimon Larsson
588112
588112
add a comment |
add a comment |
Thanks for contributing an answer to Data Science Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fdatascience.stackexchange.com%2fquestions%2f47953%2fhow-to-create-a-df-that-gets-sum-of-columns-based-on-a-groupby-column%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