python - 要拆分的字符串列表的每个元素
问题描述
我刚刚执行了熊猫系列对象,如下所示:
0 ['str1', 'str2', 'str3', 'str4', ...]
1 ['str5', 'str6', 'str7', 'str8', ...]
2 ['str9', 'str10', 'abcde.fghi', 'str12', ...]
.
.
.
在这里,我想保留格式,并用分隔符 '.' 分割字符串,如 'abcde.fghi'。
- 将“abcde.fghi”替换为“abcde”
- 在“abcde”之后插入“fghi”,然后是“str12”
它需要保持这种格式。我想要的是以下内容:
2 ['str9', 'str10', 'abcde', 'fghi', 'str12', ...]
这一系列的名单不仅仅是他们。这就像它们的 300000 行,所以我需要在迭代中使用 split 函数。
+++ 我在应用 nltk word_tokenize 后得到了这个系列。
lists_above=mydataframe['textcolum'].apply(word_tokenize)
但是,由于数据中有很多没有空格的句子,所以我正在尝试更多步骤
解决方案
你可以很容易地在 pandas 中使用 lambda 列表理解来做到这一点:
df['words'] = df['words'].apply(lambda x: [item for sub in x for item in sub.split('.')])
推荐阅读
- mongodb - mongod - 是否可以删除“NETWORK ...从...收到客户端元数据”之类的日志?
- visual-studio-code - 使用 global.clang-format 文件配置 VSC clang 格式
- sql - 使用 Concat 方法优化查询
- marklogic - MarkLogic Smart Mastering 不返回匹配项
- c# - 使用从数据库获取的值更改按钮文本
- html - UIkit HTML横向卡片使用
- c# - 在 ML.NET 中延迟加载位图
- sccm - SCCM 无线适配器驱动程序版本信息
- python - 在 ApacheSpark 中执行脚本后出现错误“TypeError: cannot unpack non-iterable float object”。谁能调试我的代码?
- ios - 向下滑动以关闭另一个视图控制器时,有没有办法将数据传回视图控制器?