在python中使用pandas映射带有数据帧列的关键字

2024-05-20 14:37:21 发布

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

我有一个数据帧

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

还有一张单子

^{pr2}$

实现了除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列


Tags: ofthenameinsingerdfisdescription
1条回答
网友
1楼 · 发布于 2024-05-20 14:37:21

我认为您可以使用here^{}的先前解决方案:

pat = "|".join(my_list)

df['keyvalue'] = df['Description'].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   driver

总而言之:

^{pr2}$

相关问题 更多 >