Pandas - Get a row of data when looping through groupBy results
With the following Pandas DataFrame:
>>> df = pd.DataFrame({"id":[1,2,1,2],"name":['a','b','a','c'],"val_1":[1,2,3,4],"val_2":[5,6,7,8]})
>>> df
id name val_1 val_2
0 1 a 1 5
1 2 b 2 6
2 1 a 3 7
3 2 c 4 8
I'm grouping the data frame by id and name, so I can loop through each group data one at a time:
>>> grp = df.groupby(['id','name'])
<pandas.core.groupby.groupby.DataFrameGroupBy object at 0x103334c88>
Now I'm iterating through the rows of each group and printing it out:
>>> for name,group in grp:
... for row,data in group.iterrows():
... print(data)
...
id 1
name a
val_1 1
val_2 5
Name: 0, dtype: object
id 1
name a
val_1 2
val_2 6
Name: 1, dtype: object
id 2
name b
val_1 3
val_2 7
Name: 2, dtype: object
id 2
name c
val_1 4
val_2 8
Name: 3, dtype: object
The rows I want to get are:
id name val_1 val_2
0 1 a 1 5
id name val_1 val_2
1 1 a 2 6
id name val_1 val_2
2 2 b 3 7
id name val_1 val_2
3 2 c 4 8
How can I achieve this?
python pandas
add a comment |
With the following Pandas DataFrame:
>>> df = pd.DataFrame({"id":[1,2,1,2],"name":['a','b','a','c'],"val_1":[1,2,3,4],"val_2":[5,6,7,8]})
>>> df
id name val_1 val_2
0 1 a 1 5
1 2 b 2 6
2 1 a 3 7
3 2 c 4 8
I'm grouping the data frame by id and name, so I can loop through each group data one at a time:
>>> grp = df.groupby(['id','name'])
<pandas.core.groupby.groupby.DataFrameGroupBy object at 0x103334c88>
Now I'm iterating through the rows of each group and printing it out:
>>> for name,group in grp:
... for row,data in group.iterrows():
... print(data)
...
id 1
name a
val_1 1
val_2 5
Name: 0, dtype: object
id 1
name a
val_1 2
val_2 6
Name: 1, dtype: object
id 2
name b
val_1 3
val_2 7
Name: 2, dtype: object
id 2
name c
val_1 4
val_2 8
Name: 3, dtype: object
The rows I want to get are:
id name val_1 val_2
0 1 a 1 5
id name val_1 val_2
1 1 a 2 6
id name val_1 val_2
2 2 b 3 7
id name val_1 val_2
3 2 c 4 8
How can I achieve this?
python pandas
2
What do you ultimately want to do within each loop? There may be a way to accomplish that without the explicit for-loop.
– Brad Solomon
Nov 14 '18 at 13:49
Thanks for the reply, I would like to process each group rows one at a time, I'm using the groupby to fix my input data, so it will be ordered by the logical groups, then go over all the rows of each group one by one, and calculate a new vector
– Shlomi Schwartz
Nov 14 '18 at 13:52
1
Wouldn't it be easier todf.sort_values(['id','name'])
and then go through theDataFrame
instead?
– Idlehands
Nov 14 '18 at 13:54
Sounds much better, thanks for helping a noob
– Shlomi Schwartz
Nov 14 '18 at 13:58
add a comment |
With the following Pandas DataFrame:
>>> df = pd.DataFrame({"id":[1,2,1,2],"name":['a','b','a','c'],"val_1":[1,2,3,4],"val_2":[5,6,7,8]})
>>> df
id name val_1 val_2
0 1 a 1 5
1 2 b 2 6
2 1 a 3 7
3 2 c 4 8
I'm grouping the data frame by id and name, so I can loop through each group data one at a time:
>>> grp = df.groupby(['id','name'])
<pandas.core.groupby.groupby.DataFrameGroupBy object at 0x103334c88>
Now I'm iterating through the rows of each group and printing it out:
>>> for name,group in grp:
... for row,data in group.iterrows():
... print(data)
...
id 1
name a
val_1 1
val_2 5
Name: 0, dtype: object
id 1
name a
val_1 2
val_2 6
Name: 1, dtype: object
id 2
name b
val_1 3
val_2 7
Name: 2, dtype: object
id 2
name c
val_1 4
val_2 8
Name: 3, dtype: object
The rows I want to get are:
id name val_1 val_2
0 1 a 1 5
id name val_1 val_2
1 1 a 2 6
id name val_1 val_2
2 2 b 3 7
id name val_1 val_2
3 2 c 4 8
How can I achieve this?
python pandas
With the following Pandas DataFrame:
>>> df = pd.DataFrame({"id":[1,2,1,2],"name":['a','b','a','c'],"val_1":[1,2,3,4],"val_2":[5,6,7,8]})
>>> df
id name val_1 val_2
0 1 a 1 5
1 2 b 2 6
2 1 a 3 7
3 2 c 4 8
I'm grouping the data frame by id and name, so I can loop through each group data one at a time:
>>> grp = df.groupby(['id','name'])
<pandas.core.groupby.groupby.DataFrameGroupBy object at 0x103334c88>
Now I'm iterating through the rows of each group and printing it out:
>>> for name,group in grp:
... for row,data in group.iterrows():
... print(data)
...
id 1
name a
val_1 1
val_2 5
Name: 0, dtype: object
id 1
name a
val_1 2
val_2 6
Name: 1, dtype: object
id 2
name b
val_1 3
val_2 7
Name: 2, dtype: object
id 2
name c
val_1 4
val_2 8
Name: 3, dtype: object
The rows I want to get are:
id name val_1 val_2
0 1 a 1 5
id name val_1 val_2
1 1 a 2 6
id name val_1 val_2
2 2 b 3 7
id name val_1 val_2
3 2 c 4 8
How can I achieve this?
python pandas
python pandas
edited Nov 14 '18 at 13:48
Isma
8,02742036
8,02742036
asked Nov 14 '18 at 13:41
Shlomi SchwartzShlomi Schwartz
3,2262073116
3,2262073116
2
What do you ultimately want to do within each loop? There may be a way to accomplish that without the explicit for-loop.
– Brad Solomon
Nov 14 '18 at 13:49
Thanks for the reply, I would like to process each group rows one at a time, I'm using the groupby to fix my input data, so it will be ordered by the logical groups, then go over all the rows of each group one by one, and calculate a new vector
– Shlomi Schwartz
Nov 14 '18 at 13:52
1
Wouldn't it be easier todf.sort_values(['id','name'])
and then go through theDataFrame
instead?
– Idlehands
Nov 14 '18 at 13:54
Sounds much better, thanks for helping a noob
– Shlomi Schwartz
Nov 14 '18 at 13:58
add a comment |
2
What do you ultimately want to do within each loop? There may be a way to accomplish that without the explicit for-loop.
– Brad Solomon
Nov 14 '18 at 13:49
Thanks for the reply, I would like to process each group rows one at a time, I'm using the groupby to fix my input data, so it will be ordered by the logical groups, then go over all the rows of each group one by one, and calculate a new vector
– Shlomi Schwartz
Nov 14 '18 at 13:52
1
Wouldn't it be easier todf.sort_values(['id','name'])
and then go through theDataFrame
instead?
– Idlehands
Nov 14 '18 at 13:54
Sounds much better, thanks for helping a noob
– Shlomi Schwartz
Nov 14 '18 at 13:58
2
2
What do you ultimately want to do within each loop? There may be a way to accomplish that without the explicit for-loop.
– Brad Solomon
Nov 14 '18 at 13:49
What do you ultimately want to do within each loop? There may be a way to accomplish that without the explicit for-loop.
– Brad Solomon
Nov 14 '18 at 13:49
Thanks for the reply, I would like to process each group rows one at a time, I'm using the groupby to fix my input data, so it will be ordered by the logical groups, then go over all the rows of each group one by one, and calculate a new vector
– Shlomi Schwartz
Nov 14 '18 at 13:52
Thanks for the reply, I would like to process each group rows one at a time, I'm using the groupby to fix my input data, so it will be ordered by the logical groups, then go over all the rows of each group one by one, and calculate a new vector
– Shlomi Schwartz
Nov 14 '18 at 13:52
1
1
Wouldn't it be easier to
df.sort_values(['id','name'])
and then go through the DataFrame
instead?– Idlehands
Nov 14 '18 at 13:54
Wouldn't it be easier to
df.sort_values(['id','name'])
and then go through the DataFrame
instead?– Idlehands
Nov 14 '18 at 13:54
Sounds much better, thanks for helping a noob
– Shlomi Schwartz
Nov 14 '18 at 13:58
Sounds much better, thanks for helping a noob
– Shlomi Schwartz
Nov 14 '18 at 13:58
add a comment |
2 Answers
2
active
oldest
votes
Change the print to:
print(pd.DataFrame(data).T)
1
That was my thought but it seems quite inefficient. I agree with the comment - depends on what OP wants the answer might be markedly different.
– Idlehands
Nov 14 '18 at 13:51
Yeah, I will use the sort_values method. Thanks anyway
– Shlomi Schwartz
Nov 14 '18 at 14:00
add a comment |
Try this:
In [1949]: for k in grp.groups.keys():
...: print(grp.get_group(k))
...:
id name val_1 val_2
0 1 a 1 5
2 1 a 3 7
id name val_1 val_2
1 2 b 2 6
id name val_1 val_2
3 2 c 4 8
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%2f53301626%2fpandas-get-a-row-of-data-when-looping-through-groupby-results%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Change the print to:
print(pd.DataFrame(data).T)
1
That was my thought but it seems quite inefficient. I agree with the comment - depends on what OP wants the answer might be markedly different.
– Idlehands
Nov 14 '18 at 13:51
Yeah, I will use the sort_values method. Thanks anyway
– Shlomi Schwartz
Nov 14 '18 at 14:00
add a comment |
Change the print to:
print(pd.DataFrame(data).T)
1
That was my thought but it seems quite inefficient. I agree with the comment - depends on what OP wants the answer might be markedly different.
– Idlehands
Nov 14 '18 at 13:51
Yeah, I will use the sort_values method. Thanks anyway
– Shlomi Schwartz
Nov 14 '18 at 14:00
add a comment |
Change the print to:
print(pd.DataFrame(data).T)
Change the print to:
print(pd.DataFrame(data).T)
answered Nov 14 '18 at 13:50
Franco PiccoloFranco Piccolo
1,581713
1,581713
1
That was my thought but it seems quite inefficient. I agree with the comment - depends on what OP wants the answer might be markedly different.
– Idlehands
Nov 14 '18 at 13:51
Yeah, I will use the sort_values method. Thanks anyway
– Shlomi Schwartz
Nov 14 '18 at 14:00
add a comment |
1
That was my thought but it seems quite inefficient. I agree with the comment - depends on what OP wants the answer might be markedly different.
– Idlehands
Nov 14 '18 at 13:51
Yeah, I will use the sort_values method. Thanks anyway
– Shlomi Schwartz
Nov 14 '18 at 14:00
1
1
That was my thought but it seems quite inefficient. I agree with the comment - depends on what OP wants the answer might be markedly different.
– Idlehands
Nov 14 '18 at 13:51
That was my thought but it seems quite inefficient. I agree with the comment - depends on what OP wants the answer might be markedly different.
– Idlehands
Nov 14 '18 at 13:51
Yeah, I will use the sort_values method. Thanks anyway
– Shlomi Schwartz
Nov 14 '18 at 14:00
Yeah, I will use the sort_values method. Thanks anyway
– Shlomi Schwartz
Nov 14 '18 at 14:00
add a comment |
Try this:
In [1949]: for k in grp.groups.keys():
...: print(grp.get_group(k))
...:
id name val_1 val_2
0 1 a 1 5
2 1 a 3 7
id name val_1 val_2
1 2 b 2 6
id name val_1 val_2
3 2 c 4 8
add a comment |
Try this:
In [1949]: for k in grp.groups.keys():
...: print(grp.get_group(k))
...:
id name val_1 val_2
0 1 a 1 5
2 1 a 3 7
id name val_1 val_2
1 2 b 2 6
id name val_1 val_2
3 2 c 4 8
add a comment |
Try this:
In [1949]: for k in grp.groups.keys():
...: print(grp.get_group(k))
...:
id name val_1 val_2
0 1 a 1 5
2 1 a 3 7
id name val_1 val_2
1 2 b 2 6
id name val_1 val_2
3 2 c 4 8
Try this:
In [1949]: for k in grp.groups.keys():
...: print(grp.get_group(k))
...:
id name val_1 val_2
0 1 a 1 5
2 1 a 3 7
id name val_1 val_2
1 2 b 2 6
id name val_1 val_2
3 2 c 4 8
answered Nov 14 '18 at 13:49
Mayank PorwalMayank Porwal
4,9202724
4,9202724
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.
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%2f53301626%2fpandas-get-a-row-of-data-when-looping-through-groupby-results%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
2
What do you ultimately want to do within each loop? There may be a way to accomplish that without the explicit for-loop.
– Brad Solomon
Nov 14 '18 at 13:49
Thanks for the reply, I would like to process each group rows one at a time, I'm using the groupby to fix my input data, so it will be ordered by the logical groups, then go over all the rows of each group one by one, and calculate a new vector
– Shlomi Schwartz
Nov 14 '18 at 13:52
1
Wouldn't it be easier to
df.sort_values(['id','name'])
and then go through theDataFrame
instead?– Idlehands
Nov 14 '18 at 13:54
Sounds much better, thanks for helping a noob
– Shlomi Schwartz
Nov 14 '18 at 13:58