从列表中筛选并生成子DataFrame

时间:2018-11-25 00:22:07

标签: python pandas dataframe

我有一个set:  CompanyList={'Apple','LG','Samsung'}pandas DataFrame

sales=[{'name':'Samsung Korea','model':'S1'},
   {'name':'Samsung Vienam','model':'J1'},
   {'name':'LG America','model':'L1'}
  ]
df=pd.DataFrame(sales)

我想浏览CompanyList,然后从“ sales” DataFrame生成新的Sub-DataFrame。预期结果是

dataSamsung = [{'name': 'Samsung', 'model': 'S1'},{'name': 'Samsung', 'model': 'J1'}] 

dataLG = [{'name': 'LG', 'model': 'L1'}]

我尝试过:

 customer={}
 for i in companyList:
       customer[i] = df[df.name.str.contains('i')]

但这给了我一个错误的答案。您能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

尝试apply

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

applylist comprehension