首页 > 解决方案 > 要拆分的字符串列表的每个元素

问题描述

我刚刚执行了熊猫系列对象,如下所示:

0        ['str1', 'str2', 'str3', 'str4', ...]
1        ['str5', 'str6', 'str7', 'str8', ...]
2        ['str9', 'str10', 'abcde.fghi', 'str12', ...]
.
.
.

在这里,我想保留格式,并用分隔符 '.' 分割字符串,如 'abcde.fghi'。

  1. 将“abcde.fghi”替换为“abcde”
  2. 在“abcde”之后插入“fghi”,然后是“str12”

它需要保持这种格式。我想要的是以下内容:

2        ['str9', 'str10', 'abcde', 'fghi', 'str12', ...]

这一系列的名单不仅仅是他们。这就像它们的 300000 行,所以我需要在迭代中使用 split 函数。

+++ 我在应用 nltk word_tokenize 后得到了这个系列。

lists_above=mydataframe['textcolum'].apply(word_tokenize)

但是,由于数据中有很多没有空格的句子,所以我正在尝试更多步骤

标签: pythonstringpandasloopssplit

解决方案


你可以很容易地在 pandas 中使用 lambda 列表理解来做到这一点:

df['words'] = df['words'].apply(lambda x: [item for sub in x for item in sub.split('.')])

推荐阅读