Pandas Groupby using multiple criteria on different axis
I have a df
DataFrame like :
| A | B | A_ | B_ |COMMON|
--------------------------------
0 | 1 | 3 | 0 | 1 | a |
--------------------------------
1 | 8 | 5 | 4 | 0 | a |
--------------------------------
2 | 3 | 6 | 2 | 4 | b |
--------------------------------
3 | 9 | 9 | 1 | 7 | b |
And I want to group all columns X
with X_
for all letters A,B,...
(let's say, the group is called X
as well), and group as well using COMMON
. I would like to apply later function like std()
to all the grouped values.
So the result would look like:
COMMON | A | B |
---------------------------
a |std(...)|std(...)|
---------------------------
b |std(...)|std(...)|
I have been able to group either one or the other, using df.groupby(['COMMMON'])
for one criteria and .groupby(mapping_function, axis=1)
for the other one, but how do I use them together?
Another alternative for an intermediate step would be to concatenate individual columns so that I would get:
| A | B |COMMON|
----------------------
0 | 1 | 3 |a |
---------------------
1 | 8 | 5 |a |
---------------------
2 | 3 | 6 |b |
---------------------
3 | 9 | 9 |b |
---------------------
0 | 0 | 1 |a |
---------------------
1 | 4 | 0 |a |
---------------------
2 | 2 | 4 |b |
---------------------
3 | 1 | 7 |b |
But I also don't know how to do that.
Also as you might see, I don't really care about the index.
Thank you for your help!
python pandas pandas-groupby
add a comment |
I have a df
DataFrame like :
| A | B | A_ | B_ |COMMON|
--------------------------------
0 | 1 | 3 | 0 | 1 | a |
--------------------------------
1 | 8 | 5 | 4 | 0 | a |
--------------------------------
2 | 3 | 6 | 2 | 4 | b |
--------------------------------
3 | 9 | 9 | 1 | 7 | b |
And I want to group all columns X
with X_
for all letters A,B,...
(let's say, the group is called X
as well), and group as well using COMMON
. I would like to apply later function like std()
to all the grouped values.
So the result would look like:
COMMON | A | B |
---------------------------
a |std(...)|std(...)|
---------------------------
b |std(...)|std(...)|
I have been able to group either one or the other, using df.groupby(['COMMMON'])
for one criteria and .groupby(mapping_function, axis=1)
for the other one, but how do I use them together?
Another alternative for an intermediate step would be to concatenate individual columns so that I would get:
| A | B |COMMON|
----------------------
0 | 1 | 3 |a |
---------------------
1 | 8 | 5 |a |
---------------------
2 | 3 | 6 |b |
---------------------
3 | 9 | 9 |b |
---------------------
0 | 0 | 1 |a |
---------------------
1 | 4 | 0 |a |
---------------------
2 | 2 | 4 |b |
---------------------
3 | 1 | 7 |b |
But I also don't know how to do that.
Also as you might see, I don't really care about the index.
Thank you for your help!
python pandas pandas-groupby
add a comment |
I have a df
DataFrame like :
| A | B | A_ | B_ |COMMON|
--------------------------------
0 | 1 | 3 | 0 | 1 | a |
--------------------------------
1 | 8 | 5 | 4 | 0 | a |
--------------------------------
2 | 3 | 6 | 2 | 4 | b |
--------------------------------
3 | 9 | 9 | 1 | 7 | b |
And I want to group all columns X
with X_
for all letters A,B,...
(let's say, the group is called X
as well), and group as well using COMMON
. I would like to apply later function like std()
to all the grouped values.
So the result would look like:
COMMON | A | B |
---------------------------
a |std(...)|std(...)|
---------------------------
b |std(...)|std(...)|
I have been able to group either one or the other, using df.groupby(['COMMMON'])
for one criteria and .groupby(mapping_function, axis=1)
for the other one, but how do I use them together?
Another alternative for an intermediate step would be to concatenate individual columns so that I would get:
| A | B |COMMON|
----------------------
0 | 1 | 3 |a |
---------------------
1 | 8 | 5 |a |
---------------------
2 | 3 | 6 |b |
---------------------
3 | 9 | 9 |b |
---------------------
0 | 0 | 1 |a |
---------------------
1 | 4 | 0 |a |
---------------------
2 | 2 | 4 |b |
---------------------
3 | 1 | 7 |b |
But I also don't know how to do that.
Also as you might see, I don't really care about the index.
Thank you for your help!
python pandas pandas-groupby
I have a df
DataFrame like :
| A | B | A_ | B_ |COMMON|
--------------------------------
0 | 1 | 3 | 0 | 1 | a |
--------------------------------
1 | 8 | 5 | 4 | 0 | a |
--------------------------------
2 | 3 | 6 | 2 | 4 | b |
--------------------------------
3 | 9 | 9 | 1 | 7 | b |
And I want to group all columns X
with X_
for all letters A,B,...
(let's say, the group is called X
as well), and group as well using COMMON
. I would like to apply later function like std()
to all the grouped values.
So the result would look like:
COMMON | A | B |
---------------------------
a |std(...)|std(...)|
---------------------------
b |std(...)|std(...)|
I have been able to group either one or the other, using df.groupby(['COMMMON'])
for one criteria and .groupby(mapping_function, axis=1)
for the other one, but how do I use them together?
Another alternative for an intermediate step would be to concatenate individual columns so that I would get:
| A | B |COMMON|
----------------------
0 | 1 | 3 |a |
---------------------
1 | 8 | 5 |a |
---------------------
2 | 3 | 6 |b |
---------------------
3 | 9 | 9 |b |
---------------------
0 | 0 | 1 |a |
---------------------
1 | 4 | 0 |a |
---------------------
2 | 2 | 4 |b |
---------------------
3 | 1 | 7 |b |
But I also don't know how to do that.
Also as you might see, I don't really care about the index.
Thank you for your help!
python pandas pandas-groupby
python pandas pandas-groupby
asked Nov 12 at 14:22
cduguet
311214
311214
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can reshape first by melt
with removing _
from column names (for better performance, because strip
only few values) with pivot_table
:
df = (df.rename(columns=lambda x: x.strip('_'))
.melt('COMMON')
.pivot_table(index='COMMON',columns='variable', values='value', aggfunc='std'))
print (df)
variable A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
2
Nice pivot :-) .
– W-B
Nov 12 at 14:33
add a comment |
IIUC
df.melt('COMMON').assign(variable=lambda x : x['variable'].str.rstrip('_')).
groupby(['COMMON','variable']).value.std().unstack()
Out[18]:
variable A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
add a comment |
Just groupby
h = lambda x: x[-1][0]
df.set_index('COMMON', append=True).stack().groupby(['COMMON', h]).std().unstack()
A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
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%2f53264153%2fpandas-groupby-using-multiple-criteria-on-different-axis%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can reshape first by melt
with removing _
from column names (for better performance, because strip
only few values) with pivot_table
:
df = (df.rename(columns=lambda x: x.strip('_'))
.melt('COMMON')
.pivot_table(index='COMMON',columns='variable', values='value', aggfunc='std'))
print (df)
variable A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
2
Nice pivot :-) .
– W-B
Nov 12 at 14:33
add a comment |
You can reshape first by melt
with removing _
from column names (for better performance, because strip
only few values) with pivot_table
:
df = (df.rename(columns=lambda x: x.strip('_'))
.melt('COMMON')
.pivot_table(index='COMMON',columns='variable', values='value', aggfunc='std'))
print (df)
variable A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
2
Nice pivot :-) .
– W-B
Nov 12 at 14:33
add a comment |
You can reshape first by melt
with removing _
from column names (for better performance, because strip
only few values) with pivot_table
:
df = (df.rename(columns=lambda x: x.strip('_'))
.melt('COMMON')
.pivot_table(index='COMMON',columns='variable', values='value', aggfunc='std'))
print (df)
variable A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
You can reshape first by melt
with removing _
from column names (for better performance, because strip
only few values) with pivot_table
:
df = (df.rename(columns=lambda x: x.strip('_'))
.melt('COMMON')
.pivot_table(index='COMMON',columns='variable', values='value', aggfunc='std'))
print (df)
variable A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
edited Nov 12 at 14:45
answered Nov 12 at 14:29
jezrael
319k22258336
319k22258336
2
Nice pivot :-) .
– W-B
Nov 12 at 14:33
add a comment |
2
Nice pivot :-) .
– W-B
Nov 12 at 14:33
2
2
Nice pivot :-) .
– W-B
Nov 12 at 14:33
Nice pivot :-) .
– W-B
Nov 12 at 14:33
add a comment |
IIUC
df.melt('COMMON').assign(variable=lambda x : x['variable'].str.rstrip('_')).
groupby(['COMMON','variable']).value.std().unstack()
Out[18]:
variable A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
add a comment |
IIUC
df.melt('COMMON').assign(variable=lambda x : x['variable'].str.rstrip('_')).
groupby(['COMMON','variable']).value.std().unstack()
Out[18]:
variable A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
add a comment |
IIUC
df.melt('COMMON').assign(variable=lambda x : x['variable'].str.rstrip('_')).
groupby(['COMMON','variable']).value.std().unstack()
Out[18]:
variable A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
IIUC
df.melt('COMMON').assign(variable=lambda x : x['variable'].str.rstrip('_')).
groupby(['COMMON','variable']).value.std().unstack()
Out[18]:
variable A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
answered Nov 12 at 14:30
W-B
99.7k73163
99.7k73163
add a comment |
add a comment |
Just groupby
h = lambda x: x[-1][0]
df.set_index('COMMON', append=True).stack().groupby(['COMMON', h]).std().unstack()
A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
add a comment |
Just groupby
h = lambda x: x[-1][0]
df.set_index('COMMON', append=True).stack().groupby(['COMMON', h]).std().unstack()
A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
add a comment |
Just groupby
h = lambda x: x[-1][0]
df.set_index('COMMON', append=True).stack().groupby(['COMMON', h]).std().unstack()
A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
Just groupby
h = lambda x: x[-1][0]
df.set_index('COMMON', append=True).stack().groupby(['COMMON', h]).std().unstack()
A B
COMMON
a 3.593976 2.217356
b 3.593976 2.081666
edited Nov 12 at 15:06
answered Nov 12 at 14:56
piRSquared
151k22143285
151k22143285
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%2f53264153%2fpandas-groupby-using-multiple-criteria-on-different-axis%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