Skip to content
Advertisement

Find columns where at least one row contains an alphabetical letter

Let’s say I have the following data set:

import pandas as pd

df = pd.DataFrame(
        {'A': [1, 2, 3],
         'B': ['one', 2, 3],
         'C': [4, 5, '6Y']
         })

I would like to find out – without any cumbersome for loop – which columns contain at least one case with an alphabetical letter (here: B and C). I guess the result should either be a list of booleans or indices.

Thank you for your help!

Advertisement

Answer

As a quick and simple solution, you can use replace and filter:

df.replace('(?i)[a-z]', '', regex=True).ne(df).any()

A    False
B     True
C     True
dtype: bool

df.columns[df.replace('(?i)[a-z]', '', regex=True).ne(df).any()]
# Index(['B', 'C'], dtype='object')

Another option is applying str.contains column-wise:

mask = df.astype(str).apply(
    lambda x: x.str.contains(r'[a-z]', flags=re.IGNORECASE)).any()
mask

A    False
B     True
C     True
dtype: bool

df.columns[mask]
# Index(['B', 'C'], dtype='object')
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement