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
>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