Let’s say I have the following data set:
JavaScript
x
8
1
import pandas as pd
2
3
df = pd.DataFrame(
4
{'A': [1, 2, 3],
5
'B': ['one', 2, 3],
6
'C': [4, 5, '6Y']
7
})
8
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:
JavaScript
1
10
10
1
df.replace('(?i)[a-z]', '', regex=True).ne(df).any()
2
3
A False
4
B True
5
C True
6
dtype: bool
7
8
df.columns[df.replace('(?i)[a-z]', '', regex=True).ne(df).any()]
9
# Index(['B', 'C'], dtype='object')
10
Another option is applying str.contains
column-wise:
JavaScript
1
12
12
1
mask = df.astype(str).apply(
2
lambda x: x.str.contains(r'[a-z]', flags=re.IGNORECASE)).any()
3
mask
4
5
A False
6
B True
7
C True
8
dtype: bool
9
10
df.columns[mask]
11
# Index(['B', 'C'], dtype='object')
12