【问题标题】:Interpreting the regex解释正则表达式
【发布时间】:2019-07-16 11:10:13
【问题描述】:

这是我的数据框...

Status1Day  labels
0       0.0
1   RAC 13  1.0
2   WL 14   0.0
3   GNWL 15 0.0
4   RLWL 16 0.0
5   PQWL 17 0.0
6   REGRET/ 0.0
7       0.0
8       1.0

在python中运行下面一行

df['Status1Day'].replace(regex=True, inplace=True, to_replace=r'[^W/L\d\s,].*', value= r'0')

结果是

Status1Day  labels
0       0.0
1   0   1.0
2   WL 14   0.0
3   0   0.0
4   0   0.0
5   0   0.0
6   0   0.0
7       0.0
8       1.0

有人可以帮忙看看 r'[^W/L\d\s,].*' 是做什么的吗?

【问题讨论】:

  • 请标记 Python。

标签: python regex dataframe


【解决方案1】:

[^W/L\d\s,].* 表示any character that is not W, /, L, digit, whitespace, ','(comma) and all characters after that (except \n)

基本上这行代码不会更改包含W, /, L, whitespace and ',' (comma) 的字符串的开头,并将其他所有内容更改为0。

您也可以在 regex101.com 中查看解释。

【讨论】:

    猜你喜欢
    • 2013-01-06
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多