简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM