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 remove multiple Dataframe header suffices that start with \?

I have a Dataframe data, whose headers contain multiple different suffices. The common feature among all headers is that the suffix begins with ;

Column-A\foo    Column-B\bar
some data       some data

I’m trying to get the output to look like;

Column-A        Column-B
some data       some data

I have tried;

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

data.columns = data.columns.str.replace(r'\$', '')

But this does not work.

Is there a nice way to do this or should I specify the full name of each suffix?

>Solution :

Your regex for finding a backslash and trailing characters isn’t correct.

import pandas

data = pandas.DataFrame(columns=[r'Column-A\foo',r'Column-B\bar'])
data.columns = data.columns.str.replace(r'\\.*', '', regex=True)

print(data)

Output:

Empty DataFrame
Columns: [Column-A, Column-B]
Index: []
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