【问题标题】:Split string into 2 columns, but keep the separator将字符串拆分为 2 列,但保留分隔符
【发布时间】:2021-10-04 14:01:14
【问题描述】:

我在 panda df 中有一个列,我想分成 2 列。 分隔符是一个单词(实际上是单词列表) 问题是我想保留分隔符(单词)

我当前的代码如下:

new_auditlog = auditlog['Description'].str.split("Modified| Reset | Rebooted | Created | Disabled",1, expand = True )
                auditlog["Action"] = new_auditlog[0]
                auditlog["Something"] = new_auditlog[1]
                auditlog.drop(columns=["Description"], inplace= True)

该列看起来像 enter image description here

这应该根据分隔符分成 2 列

问题是分隔词“丢失,看起来像: enter image description here

可以将分隔符保持在正常的字符串拆分中。 但是你可以在熊猫数据框中对列做同样的事情吗?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    您可以添加() 来保留分隔符,例如:

    df['column1'].str.split('(sep1|sep2|sep3)')
    

    【讨论】:

      猜你喜欢
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      相关资源
      最近更新 更多