aeapen
aeapen

Reputation: 923

How to remove numbers and Strings that occur together from a dataframe

How I can eliminate the alphabetic words from the dataframe

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

Current Code

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

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

Desired Output

Text       Association
Main         Road
Parallel      Road 

Upvotes: 3

Views: 80

Answers (1)

jezrael
jezrael

Reputation: 862481

I think you need change:

matrix ['Text'].isnumeric(),

to str.contains with regex \d for match number:

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

All together:

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

Upvotes: 2

Related Questions