在python中使用pandas将关键字映射为dataframe列

在python中使用pandas将关键字映射为dataframe列,python,pandas,dataframe,data-analysis,Python,Pandas,Dataframe,Data Analysis,我有一个数据框 DF, Name Stage Description Sri 1 Sri is one of the good singer in this two 2 Thanks for reading Ram 1 Ram is one of the good cricket player ganesh 1 good driver 和一份清单 my_list=["one","driver"] I

我有一个数据框

DF,
Name    Stage   Description
Sri     1       Sri is one of the good singer in this two
        2       Thanks for reading
Ram     1       Ram is one of the good cricket player
ganesh  1       good driver
和一份清单

my_list=["one","driver"]

I tried, names=df.loc[df["Description"].str.contains("|".join(my_list),na=False), 'Name']
实现了除keyvalue列之外的所有功能

 output_DF.
Name    Stage   Description
Sri     1       Sri is one of the good singer in this two
Ram     1       Ram is one of the good cricket player

My desired output is,
desired_DF,
Name    Stage   Description                                 keyvalue
Sri     1       Sri is one of the good singer in this two    one
        2       Thanks for reading                           
Ram     1       Ram is one of the good cricket player        one
ganesh  1       good driver                                  driver

有人帮我生成keyvalue列

我认为您可以使用前面的解决方案,然后:

总而言之:

print (df)
#     Name  Stage                                Description
#0     Sri      1  Sri is one of the good singer in this two
#1              2                         Thanks for reading
#2     Ram      1      Ram is one of the good cricket player
#3  ganesh      1                            good Driver one

my_list=["ONE","driver"]
df['Name'] = df['Name'].mask(df['Name'].str.strip() == '').ffill()

pat = "|".join(my_list).lower()

names=df.loc[df["Description"].str.lower().str.contains(pat,na=False), 'Name']

df = df[df['Name'].isin(names)]

df['keyvalue'] = (df['Description'].str.lower()
                                   .str.extract("(" + pat + ')', expand=False)
                                   .fillna(''))
print (df)
#     Name  Stage                                Description keyvalue
#0     Sri      1  Sri is one of the good singer in this two      one
#1     Sri      2                         Thanks for reading         
#2     Ram      1      Ram is one of the good cricket player      one
#3  ganesh      1                            good Driver one   driver

不,耶斯雷尔,我不想为所有行添加“一”。它必须添加精确匹配的关键字Ops,请给我一些时间。编辑了最终的预期输出,请检查什么是2个或更多的值匹配?我们可以给出第一个值
print (df)
#     Name  Stage                                Description
#0     Sri      1  Sri is one of the good singer in this two
#1              2                         Thanks for reading
#2     Ram      1      Ram is one of the good cricket player
#3  ganesh      1                            good Driver one

my_list=["ONE","driver"]
df['Name'] = df['Name'].mask(df['Name'].str.strip() == '').ffill()

pat = "|".join(my_list).lower()

names=df.loc[df["Description"].str.lower().str.contains(pat,na=False), 'Name']

df = df[df['Name'].isin(names)]

df['keyvalue'] = (df['Description'].str.lower()
                                   .str.extract("(" + pat + ')', expand=False)
                                   .fillna(''))
print (df)
#     Name  Stage                                Description keyvalue
#0     Sri      1  Sri is one of the good singer in this two      one
#1     Sri      2                         Thanks for reading         
#2     Ram      1      Ram is one of the good cricket player      one
#3  ganesh      1                            good Driver one   driver