'Excel imported dataframe column header name replacement

I imported an excel table with the original column name as such.

MAS 183 /1095 OS (NV)
123 456

Column names appeared with an extra \n at the spaces for one of them and ".1" at the back of both.

MAS 183 /1095.1 OS\n(NV).1
123 456
df.columns = df.columns.str.replace("\n", " ")
df.columns = df.columns.str.replace(".1", "")

Did a string replacement on the column header with the above command and the outcome was weird for column name with numbers. All text column name seems ok.

MAS83 095 OS (NV)
123 456


Solution 1:[1]

IIUC, you want to make the column names like in the original? Then you could try:

df.columns = df.columns.str.replace("\\n", " ", regex=False).str.split('.').str[0]

Output:

   MAS 183 /1095  OS (NV)
0            123      456

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1