Reputation: 59
I have a CSV file like this as data:
Emp_id, Name ,Address , Ph_no
e1,ABC,Place1,111
e2,DEF,"Place2, Place3",222
e3,GHI,"Place4, Place5",333
e4,JKL,Place6,444
I want the output:
Emp_id Name Address Ph_no
e1 ABC Place1 111
e2 DEF Place2, Place3 222
e3 GHI Place4, Place5 333
e4 JKL Place6 444
My code for reading the csv:
pd.read_csv(data, skipinitialspace=True, usecols=["Emp_id", "Name", "Address", "Ph_no"])
The error I'm getting for the extra trailing white spaces for the column names:
"Usecols do not match columns, columns expected but not found: ['Name', 'Address']"
How can be the extra space ignored or removed from the column names to check with usecols while reading the csv?
Upvotes: 0
Views: 1607
Reputation:
Use .str.strip
to trim whitespace from the columns after reading the dataframe:
df = pd.read_csv(data, skipinitialspace=True)
df.columns = df.columns.str.strip()
Output:
>>> df
Emp_id Name Address Ph_no
0 e1 ABC Place1 111
1 e2 DEF Place2, Place3 222
2 e3 GHI Place4, Place5 333
3 e4 JKL Place6 444
Upvotes: 1