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

Rename all columns of a dataframe removing all characters, if any, before the last slash

I have a dataframe where some, but not all, columns start with weird prefixes. For example,
ot1/pr1/cp1/dkjsakhka, gt2/pr1/fl1/ct4/rmt12/dasljdals, etc. I want to rename all columns which have slashes in their names by removing all the characters before the last slash, e.g. ot1/pr1/cp1/dkjsakhka -> dkjsakhka, gt2/pr1/fl1/ct4/rmt12/dasljdals -> dasljdals, while leaving the other column names untouched. How can I do that?

>Solution :

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

You can use str.rsplit:

df.columns = df.columns.str.rsplit('/', n=1).str[-1]

Output:

# before
>>> df.columns
Index(['untouched column', 'ot1/pr1/cp1/dkjsakhka',
       'gt2/pr1/fl1/ct4/rmt12/dasljdals'],
      dtype='object')

# after
>>> df.columns
Index(['untouched column', 'dkjsakhka', 'dasljdals'], dtype='object')

EDIT:

You can also use rename if you prefer:

df = df.rename(lambda x: x.rsplit('/', maxsplit=1)[-1], axis=1)
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