DevLoverUmar
DevLoverUmar

Reputation: 14011

How to trim Header values in pandas.read_csv

I'm reading a csv file using pandas.read_csv(). My csv file has headers with spaces at start or end like ' header1', 'header2 ' I want to trim that extra space at start/end. Is their a way to specify this using some arguments? If not how to achieve that in my resultant dataframe?

Upvotes: 4

Views: 2539

Answers (1)

MrNobody33
MrNobody33

Reputation: 6483

You could try with this, after reading the csv:

df.columns =[col.strip() for col in df.columns]

Same as:

df.rename(columns=lambda x: x.strip(), inplace=True)

Or this too:

df.columns=df.columns.str.strip()

Upvotes: 4

Related Questions