【问题标题】:Remove special character in the description column DataFrame删除描述列DataFrame中的特殊字符
【发布时间】:2021-12-15 20:41:40
【问题描述】:

我在 DataFrame 上使用正则表达式。我的表达式匹配如下行:[*, *,___,” "] 如您在此描述中所见:

df["Description"].str.extract("Localisation[\s]*:.*\n([^_\n]*)\n").value_counts()

如何修改我的表达式以删除这些行?

【问题讨论】:

  • 你说的是星号 (*) 吗?如果您可以在运行正则表达式之前放置一两行,那就太好了。
  • 这个不清楚:你想匹配什么?您想要提取哪些行?
  • 您需要更多帮助吗?

标签: python regex pandas


【解决方案1】:

试试这个:

df["Description"] = df["Description"].apply(lamda x: ''.join([s for s in x if s.isalnum() or s.isspace()]))

或者,如果您只想删除特定值:

values = ["*", "_"," "]
df["Description"] = df["Description"].apply(lamda x: ''.join([s for s in x if x not in values]))

【讨论】:

  • 我试试这张地图(lambda x:x if x not in values,df["Description"].str.extract(r"Localisation[\s]*:.*\n([^ _\n]*)\n"))
  • 我有错误消息:文件“”,第 37 行 map(lambda x:x if x not in values,df["Description"].str.extract (r"Localisation[\s]*:.*\n([^_\n]*)\n")) ^ SyntaxError: invalid syntax
猜你喜欢
  • 2023-03-04
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 2021-11-27
  • 2017-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多