Conditional column based on multiple string columns

Question:

I’m trying to get a filter done in a table, which must remove rows that contains strings "<" or ">". This filter must de applied for 13 columns, which I created a list with their names. Can anyone help me?

Fictional example, the rows that contains at least one "<" or ">" (in red) should be filtered out

Asked By: Lucas Picardi

||

Answers:

Check below code using numpy.char.find, can be applied to any number of columns.

import pandas as pd
import numpy as np 

df = pd.DataFrame({'col1':[1,2,3,4,5], 'col2':['>1',2,3,4,5,],'col3':[1,2,3,4,'<5'],'col4':[1,2,3,'<4',5]})

cond_1 = np.char.find(np.array(df.values, dtype='str'),"<")

cond_2 = np.char.find(np.array(df.values, dtype='str'),">")

df.drop(df[(np.where( (cond_1 + cond_2) <-1,False, True))].index)

Output:

enter image description here

Answered By: Abhishek