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

Pandas extract only capturing first character

I want to do an extract to capture all characters that match a regular expression and add those extracted characters to another column. I want to use extract to get all the letters in a dataframe cell except W. When I run the code below, it only captures the first character. Again I want to capture all the letters but no W and also no numbers or any dashes.

Here’s the code:

df["type"] = df["callsign"].str.extract(r'([^W0-9-])')

Currently the data frame shows the below result.

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

callsign type
1AB3-W9 A
23DC-W0 D

But I need it to produce:

callsign type
1AB3-W9 AB
23DC-W0 DC

>Solution :

Assuming you want to extract the letters right before the -W, use:

df["type"] = df["callsign"].str.extract(r'([a-zA-Z]+)-W')

For the first set of letters that are not W, you’re missing a +:

df["callsign"].str.extract(r'([^W0-9-]+)')
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