1

Here is a small sample of dataframe I would like to split into two separate dataframes.

  No    Code         Name Rem Last Done   LACP     Chg  % Chg Vol ('00)  \
0  1    0012       3A [S]   s     0.940  0.940       -      -        20   
1  2    7054    AASIA [S]   s         -  0.205       -      -         -   
2  3    5238      AAX [S]   s     0.345  0.340   0.005  +1.47    37,806   
3  4  5238WA   AAX-WA [S]   s     0.135  0.135       -      -       590   
4  5    7086  ABLEGRP [S]   s     0.095  0.100  -0.005  -5.00       300   

I want to filter on the "Code" column based on matching or not matching the following python regular expression:

"^[0-9]{1,5}$"

1 Answer 1

2

Use str.contains with boolean indexing, ~ is for inverting boolean mask:

m = df['Code'].str.contains("^[0-9]{1,5}$")

df1 = df[m]
print (df1)
   No  Code     Name  Rem Last   Done   LACP     Chg  % Chg Vol ('00)
0   1  0012       3A  [S]    s  0.940  0.940       -      -        20
1   2  7054    AASIA  [S]    s      -  0.205       -      -         -
2   3  5238      AAX  [S]    s  0.345  0.340   0.005  +1.47    37,806
4   5  7086  ABLEGRP  [S]    s  0.095  0.100  -0.005  -5.00       300

df2 = df[~m]
print (df2)
   No    Code    Name  Rem Last   Done   LACP Chg % Chg Vol ('00)
3   4  5238WA  AAX-WA  [S]    s  0.135  0.135   -     -       590

Detail:

print (m)
0     True
1     True
2     True
3    False
4     True
Name: Code, dtype: bool

print (~m)
0    False
1    False
2    False
3     True
4    False
Name: Code, dtype: bool
Sign up to request clarification or add additional context in comments.

3 Comments

I was having trouble wrapping my head around how boolean indexing would work for this application. Thank you for the illustration.
is it possible to an "or" condition? something like m = df['Code'].str.contains("^[0-9]{1,5}$" | "5235SS" )
@TimothyLombard - You can use m = df['Code'].str.contains("^[0-9]{1,5}$|5235SS")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.