【发布时间】:2020-01-31 23:51:37
【问题描述】:
我有两张 Excel 表格。一个包含摘要,另一个包含具有潜在过滤词的类别。如果第二个数据框中的任何元素匹配,我需要为第一个数据框分配类别。
我试图扩展第二个数据框中的列表,并通过将术语与第一个数据框中的任何单词进行匹配来进行映射。
测试数据。
import pandas as pd
data1 = {'Bucket':['basket', 'bushel', 'peck', 'box'], 'Summary':['This is a basket of red apples. They are sour.', 'We found a bushel of fruit. They are red and sweet.', 'There is a peck of pears that taste sweet. They are very green.', 'We have a box of plums. They are sour and have a great color.']}
data2 = {'Category':['Fruit', 'Color'], 'Filters':['apple, pear, plum, grape', 'red, purple, green']}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
print(df1)
Bucket Summary
0 basket This is a basket of red apples. They are sour.
1 bushel We found a bushel of fruit. They are red and s...
2 peck There is a peck of pears that taste sweet. The...
3 box We have a box of plums. They are sour and have...
print(df2)
Category Filters
0 Fruit apple, pear, plum, grape
1 Color red, purple, green
这行脚本将表中的 Category 列转换为列表以供以后使用。
category_list = df2['Category'].values
category_list = list(set(category_list))
尝试匹配文本。
for item in category_list:
item = df2.loc[df2['Category'] == item]
filter_list = item['Filters'].values
filter_list = list(set(filter_list))
df1 = df1 [df1 ['Summary'].isin(filter_list)]
我希望第一个数据框具有分配给它的类别,用逗号分隔。
结果:
Bucket Category Summary
0 basket Fruit, Color This is a basket of red apples. They are sour.
1 bushel Color We found a bushel of fruit. They are red and s...
2 peck Fruit, Color There is a peck of pears that taste sweet. The...
3 box Fruit We have a box of plums. They are sour and have...
我希望这很清楚。我已经用头撞了一个星期了。
提前谢谢你
【问题讨论】:
标签: python pandas dataframe filtering