How to read csv file contains pixels value of images with Pandas DataFrame

Viewed 878

I have the csv file looks like this:

im1000100101     0
im1011100101     1

The first column is pixels value of images and second column is the class of that image. How can I use pd.read_csv() to save the each pixel in a separate column. I want to my DataFrame looks like this:

px-1  px-2  px-3  px-4  px-5 px-6  px-7  px-8  px-9  px-10  label
1     0     0     0     1    0     0     1     0     1      0 
1     0     1     1     1    0     0     1     0     1      1
1 Answers

Use read_fwf:

import pandas as pd
from pandas.compat import StringIO

temp=u"""im1000100101     0
im1011100101     1"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
N = 12
df = pd.read_fwf(StringIO(temp), header=None, widths=[1] * N + [6], usecols=range(2,13))
df.columns = ['px-{}'.format(x+1) for x in df.columns[:-1]] + ['label']
print (df)
   px-1  px-2  px-3  px-4  px-5  px-6  px-7  px-8  px-9  px-10  label
0     1     0     0     0     1     0     0     1     0      1      0
1     1     0     1     1     1     0     0     1     0      1      1
Related