Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to only select the very next string after target string but the next string after the target string, regardless of punctation?

I have a df that looks like this:

id        query
1         select * from table1 where col1 = 1
2         select a.columns FROM table2 a

I want to only select the string (table if you know sql) after the string FROM into a new column. FROM can be spelled with different capitalizations (ie From, from,FROM,etc).

How do I select the string directly after the From but not the very next string after the FROM string

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried:

df['tableName'] = df['query'].str.extract('[^from]*$')

but this is not working. I am not sure if I should make the entire df lowercase right off the bat.

New df should look like this:

id        query                                 tableName      
1         select * from table1 where col1 = 1   table1
2         select a.columns FROM table2 a        table2

Thank you in advance.

>Solution :

You can try

df['tableName'] = df['query'].str.extract('(?i)from ([^ ]*)')

(?i) means ignore case.

print(df)

   id                                query tableName
0   1  select * from table1 where col1 = 1    table1
1   2       select a.columns FROM table2 a    table2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading