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;
}







0















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?










share|improve this question

























  • 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













  • it works after fixing, thanks @jpp

    – HTB
    Nov 26 '18 at 18:26


















0















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?










share|improve this question

























  • 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













  • it works after fixing, thanks @jpp

    – HTB
    Nov 26 '18 at 18:26














0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 1:32









U9-Forward

18.6k51744




18.6k51744










asked Nov 25 '18 at 0:22









HTBHTB

41




41













  • 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













  • 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











  • 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

















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












1 Answer
1






active

oldest

votes


















0














Try apply:



df['name']=df['name'].apply(lambda x: [i for i in CompanyList if i in x][0])


apply with list comprehension.






share|improve this answer
























  • 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












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
});


}
});














draft saved

draft discarded


















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









0














Try apply:



df['name']=df['name'].apply(lambda x: [i for i in CompanyList if i in x][0])


apply with list comprehension.






share|improve this answer
























  • 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
















0














Try apply:



df['name']=df['name'].apply(lambda x: [i for i in CompanyList if i in x][0])


apply with list comprehension.






share|improve this answer
























  • 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














0












0








0







Try apply:



df['name']=df['name'].apply(lambda x: [i for i in CompanyList if i in x][0])


apply with list comprehension.






share|improve this answer













Try apply:



df['name']=df['name'].apply(lambda x: [i for i in CompanyList if i in x][0])


apply with list comprehension.







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()