如何从数据框中删除字母词
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/