繁体   English   中英

python 从 python dataframe 列中的长字符串中删除变量 substring 模式

[英]python remove variable substring patterns from a long string in a python dataframe column

我的 dataframe 中有一个列,其中包含非常大的字符串。 这是字符串的简短示例

FixedChar{3bf3423 要保留的数据}, FixedChar{5e0d20 要保留的数据}, FixedChar{6cb86d9 要保留的数据}, ...

我需要删除重复出现的 static "FixedChar{" 和变量 substring 之后,它具有 static 长度为 6 并且还保留"到可变长度的字符串"并保留 " 删除这种重复变量模式的最佳方法是什么?

这比我想象的要容易。 起初我开始使用re库中的re.sub() 正则表达式\w*删除“FixedChar”之后的所有单词字符(字母和数字),并且参数flags = re.I使其不区分大小写。

import re
re.sub(r"FixedChar{\w*","",dataFrame.Column[row],flags = re.I)

但我发现 str.replace() 更有用,并使用 loc 替换了 dataFrame 中的值,因为我需要过滤 dataframe 导致此模式仅显示在特定行中。

dataFrame.loc['Column'] = dataFrame.Column.str.replace("FixedChar{\w* ",'',regex=True)
dataFrame.loc['Column'] = dataFrame.Column.str.replace("}",'',regex=True)

暂无
暂无

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

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