使用其他数据帧列表的值筛选数据帧

2024-05-16 00:58:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下两个数据帧:

c_pd = pd.DataFrame({'Name': ['Geeks', 'Peter', 'James', 'Jack', 'Lisa'], 
               'england': ['fish and chips','cheese hamburger, roast beef, whatever','pizza peperoni, pizza marinera, wine','steak with french fries','potetos with tomato'], 
               'france': ['voiture, maison, petit dejeuner','voiture blanc, grand maison, whatever','ratatouille, vin','fromage','petit fromage']}) 

a_df = pd.DataFrame({'groups1':[['fromage blanc grand', 'petit dejeuner'],['la vache qui rie']],'groups2':[['Coq au vin','coq a la bier'],['cannard, vin']]}, index=['paragraph1','paragraph2'])

目视查看结尾处的DFs图片,获得预期结果

我想在第二个DF b_DF中添加一列

land='france'
a_df['groups_1_dishes']=a_df['groups_1'].apply(lambda x: f(x,c_pd,land))

我想应用一个函数来执行以下操作。 对于列组_1中的每个元素,我想在组_1中创建一个列,列中包含c_pd的名称列表,该列表符合组_1中任何元素(逗号=分隔符)的所有单词都包含在相应的名称对中的条件

例如: 对于组1的第一个元素,我们有两个列表元素,“白奶酪”和“红牛排”。表1中的任何一个人都访问过英国和法国。列出了他最喜欢的食物/碟子。我必须弄清楚“英格兰”一栏中是否有“白色”和“奶酪”这两个词。是吗?是的,彼得吃红牛排,杰克吃白奶酪。第一个结果就是名单[彼得,杰克] 让我们来做第二个例子。对于a_df.loc[['paragraph2'],['groups1']]我们有[牛排、沙拉、苹果香蕉],因此牛排在彼得列表中(记住,对于a_df.loc中的每一个元素,英格兰字符串中的所有单词)。沙拉是不存在的,“苹果”和“香蕉”都不是。所以结果就是[彼得]

这个功能在陆地上,因为我想对法国做同样的事情,进一步对groups2和这两个陆地,等等

预期产量 请参见添加了一个_df['group_1_names']的新列 enter image description here

到目前为止,我试过:

def f(list_of_disches,c_pd, land):
        
    # for instance the list_of_dishes would be here: ['fromage blanc grand', 'petit dejeuner']
    
    names_found=[]
    
    for dish in list_of_disches:
        # if all the words of sintagma are in concept add concept
        dish_words=dish.split() # for instance it would be ['fromage' 'blanc' 'grand']
        
        mask = c_pd[land].apply(any(lambda x: word in x for word in dish_words))
        
        names = c_pd[mask]['name'].tolist()

        names_found.append(names)

我想我离这儿不远,但我办不到

添加编辑:预期输出


Tags: ofin元素df列表fornamespd