简体   繁体   English

如何使用Pandas DataFrame读取CSV文件包含图像的像素值

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

I have the csv file looks like this: 我的csv文件如下所示:

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. 如何使用pd.read_csv()将每个像素保存在单独的列中。 I want to my DataFrame looks like this: 我想我的DataFrame看起来像这样:

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 : 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何读取python pandas中包含字典的.csv文件? - How to read .csv file that contains dictionaries in python pandas? Pandas read_csv():列包​​含数据帧但被读取为字符串 - Pandas read_csv(): column contains dataframe but is read as string 如何将csv文件读取到具有多行索引级别的pandas DataFrame中? - How to read csv file into pandas DataFrame with multiple row index level? 如何使用 a.SAS 或 SPS 元数据文件来读取 CSV 作为 Pandas Z6A8064B5DF47945557DZC5053? - How to use a .SAS or SPS metadata file to read a CSV as a Pandas dataframe? 如何将这样的非结构化 csv 文件读取到 pandas dataframe? - How to read unstructured csv file like this to pandas dataframe? 如何在 Pandas 中将 csv 文件作为系列而不是数据帧读取? - How to read a csv file as series instead of dataframe in pandas? 如何在streamlit中从用户读取csv文件并转换为pandas数据帧 - How to read csv file from user in streamlit and converting to pandas dataframe csv to pandas 数据框包含分号 - csv to pandas dataframe contains semicolon 如何使用pandas.read_csv读取csv文件时将pandas.dataframe中的元素转换为np.float? - How to convert the element in a pandas.dataframe to np.float while use pandas.read_csv to read csv file? 将 CSV 文件读入 Pandas Dataframe 分块导致单个目标 Dataframe - Read CSV File into Pandas Dataframe with Chunking Resulting in a Single Target Dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM