我正在尝试使用字典对列表中的元素进行分类:
mydict = {'beach': ['beach', 'sand', 'coast'], 'package': ['package', 'inclusive']}
给出数据框:
Keyword |cat |
--------------------------------|----|
beach holiday | |
package beach holiday | |
inclusive beach holiday | |
我想检查一个元素是否在字典中,它将关键字应用于类别列,例如:
Keyword |cat |
--------------------------------|----|
beach holiday |beach |
package beach holiday |package|
inclusive package beach holiday |package|
我尝试使用以下代码:
df = get_csv(csv)
mydict = {'beach': ['beach', 'sand', 'coast'], 'package': ['package', 'inclusive']}
for key in mydict.keys():
item = key
if item in mydict[key]:
target_cats = item
find_keywords = lambda kw: [s for s in kw.split() if s in target_cats]
df.loc[:, 'cat_list'] = df['Keyword'].apply(lambda x: find_keywords(x))
for i in range(1, 4):
df.loc[:, 'cat{0}'.format(i)] = df['cat_list'].apply(lambda x: x[i-1] if len(x) >= i else '')
print(df)
df.to_csv('kuoniTesting.csv')
然而,这只是一个空的类别列表,检查列表的代码是否有效,如何修改它以使用字典?
target_cats = ['cat', 'dog', 'cow']
df = pd.DataFrame({'Keyword': ['cat dog cow', 'cat dog', 'dog sheep']})
find_keywords = lambda kw: [s for s in kw.split() if s in target_cats]
df.loc[:, 'cat_list'] = df['Keyword'].apply(lambda x: find_keywords(x))
for i in range(1, 4):
df.loc[:, 'cat{0}'.format(i)] = df['cat_list'].apply(lambda x: x[i-1] if
len(x) >= i else '')
Keyword cat_list cat1 cat2 cat3
0 cat dog cow [cat, dog, cow] cat dog cow
1 cat dog [cat, dog] cat dog
2 dog sheep [dog] dog
答案 0 :(得分:1)
您可以使用键在字典中交换values
,但它会返回多个值:
mydict = {'beach': ['beach', 'sand', 'coast'], 'package': ['package', 'inclusive']}
d = {k: oldk for oldk, oldv in mydict.items() for k in oldv}
print (d)
{'sand': 'beach', 'package': 'package', 'beach': 'beach',
'inclusive': 'package', 'coast': 'beach'}
find_keywords = lambda kw: [d[s] for s in kw.split() if s in d.keys()]
df['cat_list'] = df['Keyword'].apply(lambda x: find_keywords(x))
print (df)
Keyword cat_list
0 beach holiday [beach]
1 package beach holiday [package, beach]
2 inclusive beach holiday [package, beach]
对于新列:
df = df.join(pd.DataFrame(df['cat_list'].values.tolist(), columns=['cat1','cat2']))
print (df)
Keyword cat_list cat1 cat2
0 beach holiday [beach] beach None
1 package beach holiday [package, beach] package beach
2 inclusive beach holiday [package, beach] package beach