• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在数据列拆分值并其添加到带有pandas条件的新列

用户头像
it1352
帮助1

问题说明

我有一个df,

name                        Value
Sri is a cricketer          Sri,is
Ram player                  Ram
Ravi is a singer            is
cricket and foot is ball    and,is,foot

和一个列表

my_list=["is", "foot"]

我正在尝试将df ["value"]除以(,),并将该值添加到新列(如果my_list中存在该值). 我的预期输出是

I am trying to split df["value"] by (,) and adding the value to a new column if the value exists in my_list. My expected output is

name                      Value        my_list
Sri is a cricketer        Sri           is      
Ram player                Ram 
Ravi is a singer                        is     
cricket and foot is ball  and          is,foot

请帮助实现这一目标,在此先感谢

please help to achieve this, thanks in advance

正确答案

#1

使用 str.join :

my_list=["is", "foot"]
df['my_list'] = df['Value'].str.findall('('   '|'.join(my_list)   ')').str.join(',')
print (df)
                       name        Value  my_list
0        Sri is a cricketer       Sri,is       is
1                Ram player          Ram         
2          Ravi is a singer           is       is
3  cricket and foot is ball  and,is,foot  is,foot

使用 split 并获得set个中的intersection个:

my_list=["is", "foot"]
df['my_list']=df['Value'].str.split(',').apply(lambda x: set(x) & set(my_list)).str.join(',')
print (df)
                       name        Value  my_list
0        Sri is a cricketer       Sri,is       is
1                Ram player          Ram         
2          Ravi is a singer           is       is
3  cricket and foot is ball  and,is,foot  is,foot

最后:

df['Value'] = (df['Value'].str.replace('('   '|,'.join(my_list)   ')', '')
                          .str.replace('[,]{2,}',',')
                          .str.strip(','))
print (df)
                       name Value  my_list
0        Sri is a cricketer   Sri       is
1                Ram player   Ram         
2          Ravi is a singer             is
3  cricket and foot is ball   and  is,foot

或者:

my_list=["is", "foot"]

s1 = df['Value'].str.split(',')

df['my_list'] = s1.apply(lambda x: set(x) & set(my_list)).str.join(',')
df['Value'] = s1.apply(lambda x: set(x) - set(my_list)).str.join(',')
print (df)

                       name Value  my_list
0        Sri is a cricketer   Sri       is
1                Ram player   Ram         
2          Ravi is a singer             is
3  cricket and foot is ball   and  is,foot

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanheccjbk
系列文章
更多 icon
同类精品
更多 icon
继续加载