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 split column if condition, else null

I want to split a column. If it has a letter (any letter) at the end, this will be the value for the second column. Otherwise, the second column should be null

import pandas as pd
data = pd.DataFrame({"data": ["0.00I", "0.01E", "99.99", "0.14F"]})

desired result:

    a    b
0 0.00   I
1 0.01   E
2 99.99  None
3 0.14   F

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

>Solution :

You can use str.extract with the (\d+(?:\.\d+)?)(\D)? regex:

out = data['data'].str.extract(r'(\d+(?:\.\d+)?)(\D)?').set_axis(['a', 'b'], axis=1)

Or, if you want to remove the original ‘data’ column while adding new columns in place:

data[['a', 'b']] = data.pop('data').str.extract('(\d+(?:\.\d+)?)(\D)?')

output:

       a    b
0   0.00    I
1   0.01    E
2  99.99  NaN
3   0.14    F

regex demo

(\d+(?:\.\d+)?)  # capture a number (with optional decimal)
(\D)?            # optionally capture a non-digit
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