繁体   English   中英

如何部分替换列中的字符串 pandas Dataframe python

[英]How to partially replace a string in a column pandas Dataframe python

我有一个这样的 dataframe:

  Name          Comment
1 Ama           Delay with coming home client to provide place he visited
2 Kofi          Enquiry on apples client to provide refund 
3 Kwame         Equiry on tables client advised with enquiry
4 Theo          Emotional challenge client to provide details
5 Isaiah        Eating on empty stomach client to confirm the issues

但我需要它看起来像这样:

  Name          Comment
1 Ama           Delay with coming home
2 Kofi          Enquiry on apples 
3 Kwame         Enquiry on tables 
4 Theo          Emotional challenge 
5 Isaiah        Eating on empty stomach 

看起来您想删除“client”之后的所有内容,使用带有str.replace的正则表达式:

df['Comment'] = df['Comment'].str.replace(r'\s*\bclient\b.*', '',
                                          case=False, regex=True)

Output:

     Name                  Comment
1     Ama   Delay with coming home
2    Kofi        Enquiry on apples
3   Kwame         Equiry on tables
4    Theo      Emotional challenge
5  Isaiah  Eating on empty stomach

正则表达式演示

正则表达式:

\s*       # match 0 or more spaces
\b        # match word boundary
client    # match "client"
\b        # match word boundary
.*        # match anything until the end

排除以“Client”开头的字符串:

使用相同的正则表达式,但添加后视以匹配非空格: (?<=\S)

df['Comment'] = df['Comment'].str.replace(r'(?<=\S)\s*\bclient.*', '',
                                          case=False, regex=True)

例子:

     Name                   Comment
1     Ama    Delay with coming home
2    Kofi         Enquiry on apples
3   Kwame          Equiry on tables
4    Theo       Emotional challenge
5  Isaiah   Eating on empty stomach
6  Alfred  Client starting sentence

注册演示

例子

data = [['Ama', 'Delay with coming home client to provide place he visited'], 
        ['Kofi', 'Enquiry on apples client to provide refund '], 
        ['Kwame', 'Equiry on tables client advised with enquiry'], 
        ['Theo', 'Emotional challenge client to provide details'], 
        ['Isaiah', 'Eating on empty stomach client to confirm the issues'], 
        ['Amy', 'client is smart']]
df = pd.DataFrame(data, columns=['Name', 'Comment'])

df

    Name    Comment
0   Ama     Delay with coming home client to provide place...
1   Kofi    Enquiry on apples client to provide refund
2   Kwame   Equiry on tables client advised with enquiry
3   Theo    Emotional challenge client to provide details
4   Isaiah  Eating on empty stomach client to confirm the ...
5   Amy     client is smart

代码

按“客户”拆分并先取

df['Comment'] = df['Comment'].str.split(' client').str[0]

df

    Name    Comment
0   Ama     Delay with coming home
1   Kofi    Enquiry on apples
2   Kwame   Equiry on tables
3   Theo    Emotional challenge
4   Isaiah  Eating on empty stomach
5   Amy     client is smart

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM