Filter and Generate Sub-DataFrame From a List
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a set
:
CompanyList={'Apple','LG','Samsung'}
and a pandas
DataFrame
:
sales=[{'name':'Samsung Korea','model':'S1'},
{'name':'Samsung Vienam','model':'J1'},
{'name':'LG America','model':'L1'}
]
df=pd.DataFrame(sales)
I'd like to go through the CompanyList, then generate new Sub-DataFrame from 'sales' DataFrame. The expected results are
dataSamsung = [{'name': 'Samsung', 'model': 'S1'},{'name': 'Samsung', 'model': 'J1'}]
dataLG = [{'name': 'LG', 'model': 'L1'}]
I tried:
customer={}
for i in companyList:
customer[i] = df[df.name.str.contains('i')]
but this gives me a wrong answer. Could you help me to fix this case?
python pandas dataframe
add a comment |
I have a set
:
CompanyList={'Apple','LG','Samsung'}
and a pandas
DataFrame
:
sales=[{'name':'Samsung Korea','model':'S1'},
{'name':'Samsung Vienam','model':'J1'},
{'name':'LG America','model':'L1'}
]
df=pd.DataFrame(sales)
I'd like to go through the CompanyList, then generate new Sub-DataFrame from 'sales' DataFrame. The expected results are
dataSamsung = [{'name': 'Samsung', 'model': 'S1'},{'name': 'Samsung', 'model': 'J1'}]
dataLG = [{'name': 'LG', 'model': 'L1'}]
I tried:
customer={}
for i in companyList:
customer[i] = df[df.name.str.contains('i')]
but this gives me a wrong answer. Could you help me to fix this case?
python pandas dataframe
Thats aset
not alist
!
– U9-Forward
Nov 25 '18 at 0:22
This is a typo. Usei
not'i'
, the latter is just a string, the former references an element incompanyList
.
– jpp
Nov 25 '18 at 1:37
it works after fixing, thanks @jpp
– HTB
Nov 26 '18 at 18:26
add a comment |
I have a set
:
CompanyList={'Apple','LG','Samsung'}
and a pandas
DataFrame
:
sales=[{'name':'Samsung Korea','model':'S1'},
{'name':'Samsung Vienam','model':'J1'},
{'name':'LG America','model':'L1'}
]
df=pd.DataFrame(sales)
I'd like to go through the CompanyList, then generate new Sub-DataFrame from 'sales' DataFrame. The expected results are
dataSamsung = [{'name': 'Samsung', 'model': 'S1'},{'name': 'Samsung', 'model': 'J1'}]
dataLG = [{'name': 'LG', 'model': 'L1'}]
I tried:
customer={}
for i in companyList:
customer[i] = df[df.name.str.contains('i')]
but this gives me a wrong answer. Could you help me to fix this case?
python pandas dataframe
I have a set
:
CompanyList={'Apple','LG','Samsung'}
and a pandas
DataFrame
:
sales=[{'name':'Samsung Korea','model':'S1'},
{'name':'Samsung Vienam','model':'J1'},
{'name':'LG America','model':'L1'}
]
df=pd.DataFrame(sales)
I'd like to go through the CompanyList, then generate new Sub-DataFrame from 'sales' DataFrame. The expected results are
dataSamsung = [{'name': 'Samsung', 'model': 'S1'},{'name': 'Samsung', 'model': 'J1'}]
dataLG = [{'name': 'LG', 'model': 'L1'}]
I tried:
customer={}
for i in companyList:
customer[i] = df[df.name.str.contains('i')]
but this gives me a wrong answer. Could you help me to fix this case?
python pandas dataframe
python pandas dataframe
edited Nov 25 '18 at 1:32
U9-Forward
18.6k51744
18.6k51744
asked Nov 25 '18 at 0:22
HTBHTB
41
41
Thats aset
not alist
!
– U9-Forward
Nov 25 '18 at 0:22
This is a typo. Usei
not'i'
, the latter is just a string, the former references an element incompanyList
.
– jpp
Nov 25 '18 at 1:37
it works after fixing, thanks @jpp
– HTB
Nov 26 '18 at 18:26
add a comment |
Thats aset
not alist
!
– U9-Forward
Nov 25 '18 at 0:22
This is a typo. Usei
not'i'
, the latter is just a string, the former references an element incompanyList
.
– jpp
Nov 25 '18 at 1:37
it works after fixing, thanks @jpp
– HTB
Nov 26 '18 at 18:26
Thats a
set
not a list
!– U9-Forward
Nov 25 '18 at 0:22
Thats a
set
not a list
!– U9-Forward
Nov 25 '18 at 0:22
This is a typo. Use
i
not 'i'
, the latter is just a string, the former references an element in companyList
.– jpp
Nov 25 '18 at 1:37
This is a typo. Use
i
not 'i'
, the latter is just a string, the former references an element in companyList
.– jpp
Nov 25 '18 at 1:37
it works after fixing, thanks @jpp
– HTB
Nov 26 '18 at 18:26
it works after fixing, thanks @jpp
– HTB
Nov 26 '18 at 18:26
add a comment |
1 Answer
1
active
oldest
votes
Try apply
:
df['name']=df['name'].apply(lambda x: [i for i in CompanyList if i in x][0])
apply
with list comprehension
.
Thanks for your help, but can we separate the original DataFrame into smaller ones based on the name?
– HTB
Nov 26 '18 at 18:03
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%2f53463609%2ffilter-and-generate-sub-dataframe-from-a-list%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
Try apply
:
df['name']=df['name'].apply(lambda x: [i for i in CompanyList if i in x][0])
apply
with list comprehension
.
Thanks for your help, but can we separate the original DataFrame into smaller ones based on the name?
– HTB
Nov 26 '18 at 18:03
add a comment |
Try apply
:
df['name']=df['name'].apply(lambda x: [i for i in CompanyList if i in x][0])
apply
with list comprehension
.
Thanks for your help, but can we separate the original DataFrame into smaller ones based on the name?
– HTB
Nov 26 '18 at 18:03
add a comment |
Try apply
:
df['name']=df['name'].apply(lambda x: [i for i in CompanyList if i in x][0])
apply
with list comprehension
.
Try apply
:
df['name']=df['name'].apply(lambda x: [i for i in CompanyList if i in x][0])
apply
with list comprehension
.
answered Nov 25 '18 at 0:26
U9-ForwardU9-Forward
18.6k51744
18.6k51744
Thanks for your help, but can we separate the original DataFrame into smaller ones based on the name?
– HTB
Nov 26 '18 at 18:03
add a comment |
Thanks for your help, but can we separate the original DataFrame into smaller ones based on the name?
– HTB
Nov 26 '18 at 18:03
Thanks for your help, but can we separate the original DataFrame into smaller ones based on the name?
– HTB
Nov 26 '18 at 18:03
Thanks for your help, but can we separate the original DataFrame into smaller ones based on the name?
– HTB
Nov 26 '18 at 18:03
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%2f53463609%2ffilter-and-generate-sub-dataframe-from-a-list%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
Thats a
set
not alist
!– U9-Forward
Nov 25 '18 at 0:22
This is a typo. Use
i
not'i'
, the latter is just a string, the former references an element incompanyList
.– jpp
Nov 25 '18 at 1:37
it works after fixing, thanks @jpp
– HTB
Nov 26 '18 at 18:26