I need a way to search within a pandas grouped dataframe
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a dataset sort of like the following in an excel file named data.xlsx
Building name salary
00Apple032 Bob 50000
00Apple032 James 30000
0Bean032 James 30000
0Soda987 Alex 0
0Bean032 Bryon 32000
ive created two data frames from the data.
df = pd.read_excel('data.xlsx', sheet_name='Sheet1, dtype=str)
grouped_df = pd.read_excel('data.xlsx', sheet_name='Sheet1, dtype=str)
grouped = grouped_df.groupby("Building")[["Building", "name", "salary']]
Before I go on here .. the reason why I created the original df as well as grouped_df (which are both data frames) ... I'm not certain that applying the grouping function to the original df and assigning that to a new variable on a single line of code (like the below line) would somehow mess with the original data and cause trouble down the line. That may be inaccurate.
grouped = df.groupby("Builing")[["Building", "name", "salary']]
Anywho. This grouped dataframe is not like a normal dataframe to my knowledge. The type is listed as
pandas.core.groupby.groupby.DataFrameGroupBy
On the normal dataframe that is not grouped I can do something like this:
x = input("search for: ")
df[df['Building'].str.contains(x)]]
however on a grouped dataframe this doesn't work.
My problem that im trying to work around is this - i need to permit the ability to search in this grouped data frame to print the groups, but the user doesn't know the precise or exact group name. Was it Apples im searching for? Or 00Apples .. you can see the problem.
While i can query the data with this:
grouped.get_group('00Apples032')
I don't have a way to offer someone the ability to search for that group via something like a str.contains.
What I have tried
grouped[grouped["Building"].str.contains("Apples")]
ERROR
exception: Columns already selected
python pandas dataframe
add a comment |
I have a dataset sort of like the following in an excel file named data.xlsx
Building name salary
00Apple032 Bob 50000
00Apple032 James 30000
0Bean032 James 30000
0Soda987 Alex 0
0Bean032 Bryon 32000
ive created two data frames from the data.
df = pd.read_excel('data.xlsx', sheet_name='Sheet1, dtype=str)
grouped_df = pd.read_excel('data.xlsx', sheet_name='Sheet1, dtype=str)
grouped = grouped_df.groupby("Building")[["Building", "name", "salary']]
Before I go on here .. the reason why I created the original df as well as grouped_df (which are both data frames) ... I'm not certain that applying the grouping function to the original df and assigning that to a new variable on a single line of code (like the below line) would somehow mess with the original data and cause trouble down the line. That may be inaccurate.
grouped = df.groupby("Builing")[["Building", "name", "salary']]
Anywho. This grouped dataframe is not like a normal dataframe to my knowledge. The type is listed as
pandas.core.groupby.groupby.DataFrameGroupBy
On the normal dataframe that is not grouped I can do something like this:
x = input("search for: ")
df[df['Building'].str.contains(x)]]
however on a grouped dataframe this doesn't work.
My problem that im trying to work around is this - i need to permit the ability to search in this grouped data frame to print the groups, but the user doesn't know the precise or exact group name. Was it Apples im searching for? Or 00Apples .. you can see the problem.
While i can query the data with this:
grouped.get_group('00Apples032')
I don't have a way to offer someone the ability to search for that group via something like a str.contains.
What I have tried
grouped[grouped["Building"].str.contains("Apples")]
ERROR
exception: Columns already selected
python pandas dataframe
For your example dataset, what is your expected output?
– Paul H
Nov 29 '18 at 6:25
add a comment |
I have a dataset sort of like the following in an excel file named data.xlsx
Building name salary
00Apple032 Bob 50000
00Apple032 James 30000
0Bean032 James 30000
0Soda987 Alex 0
0Bean032 Bryon 32000
ive created two data frames from the data.
df = pd.read_excel('data.xlsx', sheet_name='Sheet1, dtype=str)
grouped_df = pd.read_excel('data.xlsx', sheet_name='Sheet1, dtype=str)
grouped = grouped_df.groupby("Building")[["Building", "name", "salary']]
Before I go on here .. the reason why I created the original df as well as grouped_df (which are both data frames) ... I'm not certain that applying the grouping function to the original df and assigning that to a new variable on a single line of code (like the below line) would somehow mess with the original data and cause trouble down the line. That may be inaccurate.
grouped = df.groupby("Builing")[["Building", "name", "salary']]
Anywho. This grouped dataframe is not like a normal dataframe to my knowledge. The type is listed as
pandas.core.groupby.groupby.DataFrameGroupBy
On the normal dataframe that is not grouped I can do something like this:
x = input("search for: ")
df[df['Building'].str.contains(x)]]
however on a grouped dataframe this doesn't work.
My problem that im trying to work around is this - i need to permit the ability to search in this grouped data frame to print the groups, but the user doesn't know the precise or exact group name. Was it Apples im searching for? Or 00Apples .. you can see the problem.
While i can query the data with this:
grouped.get_group('00Apples032')
I don't have a way to offer someone the ability to search for that group via something like a str.contains.
What I have tried
grouped[grouped["Building"].str.contains("Apples")]
ERROR
exception: Columns already selected
python pandas dataframe
I have a dataset sort of like the following in an excel file named data.xlsx
Building name salary
00Apple032 Bob 50000
00Apple032 James 30000
0Bean032 James 30000
0Soda987 Alex 0
0Bean032 Bryon 32000
ive created two data frames from the data.
df = pd.read_excel('data.xlsx', sheet_name='Sheet1, dtype=str)
grouped_df = pd.read_excel('data.xlsx', sheet_name='Sheet1, dtype=str)
grouped = grouped_df.groupby("Building")[["Building", "name", "salary']]
Before I go on here .. the reason why I created the original df as well as grouped_df (which are both data frames) ... I'm not certain that applying the grouping function to the original df and assigning that to a new variable on a single line of code (like the below line) would somehow mess with the original data and cause trouble down the line. That may be inaccurate.
grouped = df.groupby("Builing")[["Building", "name", "salary']]
Anywho. This grouped dataframe is not like a normal dataframe to my knowledge. The type is listed as
pandas.core.groupby.groupby.DataFrameGroupBy
On the normal dataframe that is not grouped I can do something like this:
x = input("search for: ")
df[df['Building'].str.contains(x)]]
however on a grouped dataframe this doesn't work.
My problem that im trying to work around is this - i need to permit the ability to search in this grouped data frame to print the groups, but the user doesn't know the precise or exact group name. Was it Apples im searching for? Or 00Apples .. you can see the problem.
While i can query the data with this:
grouped.get_group('00Apples032')
I don't have a way to offer someone the ability to search for that group via something like a str.contains.
What I have tried
grouped[grouped["Building"].str.contains("Apples")]
ERROR
exception: Columns already selected
python pandas dataframe
python pandas dataframe
asked Nov 29 '18 at 4:02
OscalationOscalation
23518
23518
For your example dataset, what is your expected output?
– Paul H
Nov 29 '18 at 6:25
add a comment |
For your example dataset, what is your expected output?
– Paul H
Nov 29 '18 at 6:25
For your example dataset, what is your expected output?
– Paul H
Nov 29 '18 at 6:25
For your example dataset, what is your expected output?
– Paul H
Nov 29 '18 at 6:25
add a comment |
1 Answer
1
active
oldest
votes
Assuming, you grouped your original dataframe on Building
:
grouped = df.groupby("Building")
This creates a groupby
object. You can loop over this object like below:
for key, value in grouped:
print(key, value)
## Do your stuff here
00Apple032
Building name salary
0 00Apple032 Bob 50000
1 00Apple032 James 30000
0Bean032
Building name salary
2 0Bean032 James 30000
4 0Bean032 Bryon 32000
0Soda987
Building name salary
3 0Soda987 Alex 0
In this, the key
will have unique Buidling names like 00Apple032
,0Bean032
, etc as shown above. And, value
will have the actual rows for each key.
So, you can treat each (key,value)
like one dataframe and check if each dataframe has x or not like this:
for key, value in grouped:
print(value[value['Building'].str.contains(x)])
## do more stuff
Let me know if this helps.
@Oscalation Let me know if this worked for you?
– Mayank Porwal
Nov 30 '18 at 4:26
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%2f53531655%2fi-need-a-way-to-search-within-a-pandas-grouped-dataframe%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
Assuming, you grouped your original dataframe on Building
:
grouped = df.groupby("Building")
This creates a groupby
object. You can loop over this object like below:
for key, value in grouped:
print(key, value)
## Do your stuff here
00Apple032
Building name salary
0 00Apple032 Bob 50000
1 00Apple032 James 30000
0Bean032
Building name salary
2 0Bean032 James 30000
4 0Bean032 Bryon 32000
0Soda987
Building name salary
3 0Soda987 Alex 0
In this, the key
will have unique Buidling names like 00Apple032
,0Bean032
, etc as shown above. And, value
will have the actual rows for each key.
So, you can treat each (key,value)
like one dataframe and check if each dataframe has x or not like this:
for key, value in grouped:
print(value[value['Building'].str.contains(x)])
## do more stuff
Let me know if this helps.
@Oscalation Let me know if this worked for you?
– Mayank Porwal
Nov 30 '18 at 4:26
add a comment |
Assuming, you grouped your original dataframe on Building
:
grouped = df.groupby("Building")
This creates a groupby
object. You can loop over this object like below:
for key, value in grouped:
print(key, value)
## Do your stuff here
00Apple032
Building name salary
0 00Apple032 Bob 50000
1 00Apple032 James 30000
0Bean032
Building name salary
2 0Bean032 James 30000
4 0Bean032 Bryon 32000
0Soda987
Building name salary
3 0Soda987 Alex 0
In this, the key
will have unique Buidling names like 00Apple032
,0Bean032
, etc as shown above. And, value
will have the actual rows for each key.
So, you can treat each (key,value)
like one dataframe and check if each dataframe has x or not like this:
for key, value in grouped:
print(value[value['Building'].str.contains(x)])
## do more stuff
Let me know if this helps.
@Oscalation Let me know if this worked for you?
– Mayank Porwal
Nov 30 '18 at 4:26
add a comment |
Assuming, you grouped your original dataframe on Building
:
grouped = df.groupby("Building")
This creates a groupby
object. You can loop over this object like below:
for key, value in grouped:
print(key, value)
## Do your stuff here
00Apple032
Building name salary
0 00Apple032 Bob 50000
1 00Apple032 James 30000
0Bean032
Building name salary
2 0Bean032 James 30000
4 0Bean032 Bryon 32000
0Soda987
Building name salary
3 0Soda987 Alex 0
In this, the key
will have unique Buidling names like 00Apple032
,0Bean032
, etc as shown above. And, value
will have the actual rows for each key.
So, you can treat each (key,value)
like one dataframe and check if each dataframe has x or not like this:
for key, value in grouped:
print(value[value['Building'].str.contains(x)])
## do more stuff
Let me know if this helps.
Assuming, you grouped your original dataframe on Building
:
grouped = df.groupby("Building")
This creates a groupby
object. You can loop over this object like below:
for key, value in grouped:
print(key, value)
## Do your stuff here
00Apple032
Building name salary
0 00Apple032 Bob 50000
1 00Apple032 James 30000
0Bean032
Building name salary
2 0Bean032 James 30000
4 0Bean032 Bryon 32000
0Soda987
Building name salary
3 0Soda987 Alex 0
In this, the key
will have unique Buidling names like 00Apple032
,0Bean032
, etc as shown above. And, value
will have the actual rows for each key.
So, you can treat each (key,value)
like one dataframe and check if each dataframe has x or not like this:
for key, value in grouped:
print(value[value['Building'].str.contains(x)])
## do more stuff
Let me know if this helps.
edited Nov 29 '18 at 6:19
answered Nov 29 '18 at 4:28
Mayank PorwalMayank Porwal
5,0182725
5,0182725
@Oscalation Let me know if this worked for you?
– Mayank Porwal
Nov 30 '18 at 4:26
add a comment |
@Oscalation Let me know if this worked for you?
– Mayank Porwal
Nov 30 '18 at 4:26
@Oscalation Let me know if this worked for you?
– Mayank Porwal
Nov 30 '18 at 4:26
@Oscalation Let me know if this worked for you?
– Mayank Porwal
Nov 30 '18 at 4:26
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%2f53531655%2fi-need-a-way-to-search-within-a-pandas-grouped-dataframe%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
For your example dataset, what is your expected output?
– Paul H
Nov 29 '18 at 6:25