0

I have a column within a dataframe that is composed of lists. I am trying to use an if statement to identify values in these lists that contain any special character or number. The numbers I am trying to identify are string values, not numeric. I have tried using regex to identify these values, but I don't know exactly how to use this in an if statement.

The code below gives me what I want, but I know there has to be a more succinct way to do it:

if '-' in row['col_name'].iloc[0] or '/' in row['col_name'].iloc[0] or '0' in row['col_name'].iloc[0] or '1' in row['col_name'].iloc[0]:

return action

I only included a few special characters and numbers in this example. I would like to find ANY special character or numeric value. Thank you in advance!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
munsond
  • 13
  • 1

1 Answers1

0

in reference to this post, the following might be what you need:

special_chars = ['-', '/', '0', '1']

# returns df with only the rows in which the column contains any of these characters
result_df = df.loc[df['col_name'].str.contains('|'.join(special_chars))]

the '|' will function as a regex character.

maxxel_
  • 437
  • 3
  • 13