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

Remove column name suffix from DataFrame Python

I have the following pandas DataFrame in python:

attr_x header example_x other
3232 322 abv ideo
342 123213 ffee iie
232 873213 ffue iie
333 4534 ffpo iieu

I want to remove the suffixes ‘_x’ from all columns containing it. The original DataFrame is much longer. Example result:

attr header example other
3232 322 abv ideo
342 123213 ffee iie
232 873213 ffue iie
333 4534 ffpo iieu

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 :

Use str.removesuffix:

df.columns = df.columns.str.removesuffix("_x")

Or replace:

df.columns = df.columns.str.replace(r'_x$', '')
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