1

I have a csv file with data in each row separated with space. I want to format these datas in columns. So if I have 5 values separated by space in a row then I want to create 5 columns. My csv file looks like following, enter image description here

I want to format them on 7 columns. I'm using pandas and trying to set the column like this

dataFrame.columns= ['Frame',  'xmin', 'xmax', 'ymin','ymax', 'occluded', 'Label']

But its showing me an error

ValueError: Length mismatch: Expected axis has 1 elements, new values have 7 elements

Tahlil
  • 2,680
  • 6
  • 43
  • 84
  • 2
    For future reference, pleace don't post screenshots of data, especially if you have text files (csv), which can easily be pasted here. Otherwise you kinda expect us to manually retype your data from the screenshot to help you. – Christian König Mar 07 '17 at 14:55

1 Answers1

4

Use separator \s+ (whitespace) or parameter delim_whitespace in read_csv:

names = ['Frame',  'xmin', 'xmax', 'ymin','ymax', 'occluded', 'Label']
df = pd.read_csv('file.csv', sep='\s+', names=names)

names = ['Frame',  'xmin', 'xmax', 'ymin','ymax', 'occluded', 'Label']
df = pd.read_csv('file.csv', delim_whitespace=True, names=names)

Error means all values are in one column, so cannot be assigned 7 column names.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252