Jack
Jack

Reputation: 1754

Select rows including integer but no letter from pandas dataframe?

Raw dataframe (df):

     A       B
0    1      green
1    2s     red
2    s      blue
3    4.3.4  yellow
4    0#     black
5    0.3    white 

Expected dataframe (df) after selecting:

     A       B
0    1      green
3    4.3.4  yellow
4    0#     black
5    0.3    white 

The type of column A is string.

I have no idea how to use regex/str.contains() to get the result.

Upvotes: 0

Views: 1245

Answers (1)

user2285236
user2285236

Reputation:

You can use a character class [a-zA-Z] to match the letters and inverse the selection with ~.

df[~df['A'].str.contains('[a-zA-Z]')]
Out: 
       A       B
0      1   green
3  4.3.4  yellow
4     0#   black
5    0.3   white

Upvotes: 3

Related Questions