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

Is there a Pandas Function that ignore columns with the same name in an input excel file

I have an excel file containing some columns with the same name and want to import it to multiple dataframes before melting data.How can i have the original columns’ names in my dataframes. The problem is that i have dataframes with columns named ex:A, A.1, A.2.
For now i am renaming the columns but wan’t a better method.

I tried renaming columns but it’s too long ( i have more than 30 columns’ that are duplicated and renamed more than 10 times) and i want a faster method or a function to do that better

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 :

There is no "out-of-the-box" way to do this, you can however remove this suffix with str.replace:

df.columns = df.columns.str.replace(r'\.\d+$', '', regex=True)

Example (with read_csv):

import pandas as pd
import io

df = pd.read_csv(io.StringIO('A,B,C,A,B,C\n1,2,3,4,5,6'))
df.columns = df.columns.str.replace(r'\.\d+$', '', regex=True)
print(df)

Output:

   A  B  C  A  B  C
0  1  2  3  4  5  6
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