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 split column values into multiple columns pandas

I have a df

Name Zone                                                    Dummy
A    BARI (BA), BARLETTA (BT), BRINDISI (BR), FOGGIA (FG)     2
B    BARI (BA), FOGGIA (FG)                                   2
C    HDEF (SE), LECCE (LE)                                    3
D    GUVA (PP)                                                3

I need df as

    Name Zone                                             Symbol            Dummy
    A    BARI , BARLETTA , BRINDISI , FOGGIA         (BA),(BT),(BR),(FG)      2
    B    BARI , FOGGIA                               (FG),(BA)                2
    C    HDEF , LECCE                                (LE),(SE)                3
    D    GUVA                                        (PP)                     3

Tried to split the Zone and Symbol using

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

Series.str.split()

but not working as expected.

>Solution :

You could use str.replace here:

df["Symbol"] = df["Zone"].str.replace(r'(?:|\s+)[A-Z]+\s+', '')
df["Zone"] = df["Zone"].str.replace(r'\s*\(.*?\)\s*', '')
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