How to delete carriage return from Pandas Headers when reading an excel file

Question:

I would like to read excel function in pandas and remove carriage returns found in the headers.

I have found multiple answers pointing out how to remove them in the data but not specifically in the headers.

I have tried a few things, see below, but not working. Any advice appreciated thanks.

df1 = pd.read_excel('S:/DataToClean.xlsm',skiprows=1)
Asked By: Jack

||

Answers:

You can use rename:

df1 = (pd.read_excel('S:/DataToClean.xlsm',skiprows=1)
         .rename(columns=lambda x: x.strip()))

# Or simply:
df1.colums = df1.columns.str.strip()
Answered By: Corralien
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.