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

Separating lower case and uppercase with a comma in Pandas Series

I have a pandas series

list_df = pd.Series(['KingsDuck',
       'RangersIslandersDevils',
       'Shark',
       'Maple Leafs',
       'Red Wing'])

display(list_df)
0                 KingsDuck
1    RangersIslandersDevils
2                     Shark
3               Maple Leafs
4                  Red Wing
dtype: object

and I would like to insert a comma between lower character and upper character. (Eg: ‘KingsDuck’ to ‘Kings,Duck’ and ‘RangersIslandersDevils’ to ‘Rangers,Islanders,Devils’)

I tried an online python regex tools to test my regex and it worked as intended:
regextesting

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

However when I tried the regex in my Jupyter Notebook, the output is not what I expected:

list_df.replace(r'(([a-z])([A-Z]))',r'\1,\2', regex=True)
0                   KingsD,suck
1    RangersI,sslandersD,sevils
2                         Shark
3                   Maple Leafs
4                      Red Wing
dtype: object

How do I go about this?

>Solution :

You have too many groups, remove the external parentheses. You have ((a)(b)) so \1 is ab, \2 is a, \3 is b.

list_df.replace(r'([a-z])([A-Z])',
                r'\1,\2', regex=True)

Or if you really want to keep the external group:

list_df.replace(r'(([a-z])([A-Z]))',
                r'\2,\3', regex=True)

Output:

0                  Kings,Duck
1    Rangers,Islanders,Devils
2                       Shark
3                 Maple Leafs
4                    Red Wing
dtype: object
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