我有一个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')]
但这给了我一个错误的答案。您能帮我解决这个问题吗?
答案 0 :(得分:0)
尝试apply
:
df['name']=df['name'].apply(lambda x: [i for i in CompanyList if i in x][0])
apply
和list comprehension
。