regex - 如何从数据框中删除一起出现的数字和字符串

标签 regex pandas

如何从数据框中删除字母词

Text       Association
  12-43       KG,JK
  12g         MG,JK
  Main Road   
  12-45        JK,TG
  f12 
  Parallel     Road 

当前代码

matrix = matrix[['Text', 'Association']]         
        import numpy as np
        conditions = [

           matrix ['Text'].isnumeric(),
        matrix ['Text'].str.len() == 1
        ]
        matrix  = matrix [~np.logical_or.reduce(conditions)]

期望的输出

Text       Association
Main         Road
Parallel      Road 

最佳答案

我认为你需要改变:

matrix ['Text'].isnumeric(),

str.contains使用正则表达式 \d 匹配 number:

matrix ['Text'].str.contains('\d'),

一起:

matrix = matrix[['Text', 'Association']]  

matrix['Text'] = matrix['Text'].astype(str)       

conditions = [

    matrix ['Text'].str.contains('\d'),
    matrix ['Text'].str.len() == 1
             ]
matrix  = matrix [~np.logical_or.reduce(conditions)]

print (matrix)
       Text Association
2      Main        Road
5  Parallel        Road

关于regex - 如何从数据框中删除一起出现的数字和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48237664/

相关文章:

正则表达式从未找到单词的位置选择困难

javascript - 更改宽度和高度 RegEx javascript

python - 基于在 "sub"Dataframe 上创建的 groupby 创建新的较小的 "root"DataFrameS

python - 以句子结构显示数据框行

ios - 用于捕获组中主题标签的正则表达式

javascript - 突出显示jquery中的当前页面

Java unicode 正则表达式不匹配德语字符

python - 如何在数据框中组合 2 个整数列并在 python 中将类型本身保持为整数

python - 使用 python 日期时间返回任意年份的特定日期、月份

excel - 使用for循环(Python)追加/连接多个excel数据集