【问题标题】:Split one colum into two by multiple delimiter characters in Python在 Python 中通过多个分隔符将一列分成两列
【发布时间】:2020-05-23 06:23:39
【问题描述】:

对于具有words 列的示例数据框,我想将每一行按llolut 拆分为两列:words1words2

                 words
0           helloworld
1          hellomadame
2           salutmonde
3          salutmadame
4    englishhelloworld
5   englishhellomadame
6   francaissalutmonde
7  francaissalutmadame

我怎样才能得到以下输出?谢谢。

          words1  words2
0          hello   world
1          hello  madame
2          salut   monde
3          salut  madame
4   englishhello   world
5   englishhello  madame
6  francaissalut   monde
7  francaissalut  madame

我尝试使用df.words.str.split('llo | lut', expand=True),但没有成功。有人可以帮忙吗?非常感谢。

                     0
0           helloworld
1          hellomadame
2           salutmonde
3          salutmadame
4    englishhelloworld
5   englishhellomadame
6   francaissalutmonde
7  francaissalutmadame

【问题讨论】:

  • 您可以通过调用Series.str.split 获得结果,请参阅this answer

标签: regex python-3.x pandas dataframe split


【解决方案1】:

使用Series.str.replace,在字符串llolut 后添加空格,然后使用Series.str.split

df = df['words'].str.replace('(llo|lut)', r'\1 ', n=1).str.split(expand=True)
df.columns=['words1','words2']
print (df)
          words1           words2
0          hello            world
1          hello           madame
2          salut            monde
3          salut           madame
4   englishhello            world
5   englishhello           madame
6  francaissalut            monde
7  francaissalut           madame

【讨论】:

  • @ahbon - 引用(llo|lut) - 在llolut 之后添加空格
  • @ahbon - 我认为问题应该是多个llolut,如果想先拆分llolut 使用df = df['words'].str.replace('(llo|lut)', r'\1 ', n=1).str.split(expand=True).add_prefix('words')
  • 得到它,如何拆分和设置名称而不是add_prefix
  • 对不起,我的数据中除了words之外还有其他列,所以我不想将df['words'].str.replace('(llo|lut)', r'\1 ', n=1).str.split(expand=True)设置为df。我可以在保留其他列的同时拆分 words 吗?
  • @ahbon - 当然,使用df[['words1','words2']] = df['words'].str.replace('(llo|lut)', r'\1 ', n=1).str.split(expand=True)
【解决方案2】:

不是一个非常 Pythonic 和高效的解决方案,但这样就可以了

df = df.words.str.split('(llo|lut)', expand=True)
df[0] = df[0] + df[1]
df = df.drop(1, axis = 1)
df = df.rename(columns = {0 : "words1", 2 : "words2"})

这将输出

    words1             words2
0   hello              world
1   hello              madame
2   salut              monde
3   salut              madame
4   englishhello       world
5   englishhello       madame
6   francaissalut      monde
7   francaissalut      madame

在重命名中,字典键必须是 0 和 2,因为连接后,数据框看起来像

    0              1    2
0   hello          llo  world
1   hello          llo  madame
2   salut          lut  monde
3   salut          lut  madame
4   englishhello   llo  world
5   englishhello   llo  madame
6   francaissalut  lut  monde
7   francaissalut  lut  madame

删除第1列后,它变成了

    0               2
0   hello           world
1   hello           madame
2   salut           monde
3   salut           madame
4   englishhello    world
5   englishhello    madame
6   francaissalut   monde
7   francaissalut   madame

列名是 0 和 2,因此完成了 0 和 2 的重命名。希望这会有所帮助!

【讨论】:

    【解决方案3】:

    只需使用单个正则表达式来拆分列:

    (?<=l(?:lo|ut))
    (?<=llo|lut)
    

    请参阅regex demo。该模式是一个正向的向后查找,它匹配紧接在llolut 之前的位置。

    Python 演示:

    import pandas as pd
    
    df = pd.DataFrame({"words": ["helloworld","hellomadame","salutmonde","salutmadame","englishhelloworld","englishhellomadame","francaissalutmonde","francaissalutmadame"]})
    
    df = df['words'].str.split(r'(?<=l(?:lo|ut))', expand=True)
    df.columns=['words1','words2']
    

    输出:

    >>> df
              words1  words2
    0          hello   world
    1          hello  madame
    2          salut   monde
    3          salut  madame
    4   englishhello   world
    5   englishhello  madame
    6  francaissalut   monde
    7  francaissalut  madame
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 2021-12-10
      • 2023-01-13
      相关资源
      最近更新 更多