0

I have a CSV file generated by some program. I am trying to read CSV using panda:

import pandas as pd
file = pd.read_csv('test.csv')

It's reading it successfully and I can display columns using file.columns

output: `Index(['col1', ' \tcol2', ' \tcol3',
       ' \tcol4', ' \tcol5', ' \tcol6', ' \tcol7',
       ' \tcol8', ' \tcol9', ' \tcol10', ' \tcol11',
       ' \tcol12', ' \tcol13'],
      dtype='object')`

I can read the first col using column_1 = file.col1 But when I am trying to read any other column, it's giving me an error as : AttributeError: 'DataFrame' object has no attribute 'col2'

I figured out that it might be due to tabs and spaces in columns, so I tried removing them using

file = file.replace(r'\r+|\n+|\t+','', regex=True) But it didn't remove anything.

I tried following as well based on many other answers:

file = file.replace(to_replace=[r"\\t|\\n|\\r", "\t|\n|\r"], value=["",""], regex=True, inplace=True)

and

file = file.replace('\t','', regex=True)

But none of them are removing anything.

A.k.
  • 192
  • 1
  • 22

2 Answers2

0

use df = df.rename(columns=lambda x: x.strip())

PSN
  • 2,326
  • 3
  • 27
  • 52
vpush
  • 11
  • 1
0

I got it using:

file = file.rename(columns=lambda x: x.strip())

It removed all white spaces. I took it from this answer: How can I strip the whitespace from Pandas DataFrame headers?

A.k.
  • 192
  • 1
  • 22