简体   繁体   English

使用pandas.read_csv读取部分标题CSV的问题

[英]Issue with reading partial header CSV using pandas.read_csv

I'm trying to read a csv file using pandas.read_csv when the files header is not full, ie, only some columns have names, others are empty. 我正在尝试使用pandas.read_csv读取csv文件,而文件头不完整,即,仅某些列具有名称,而另一些则为空。
When reading the data frame using .iloc I only get the columns which the header do not have any names. 当使用.iloc读取数据帧时,我只会得到标题没有任何名称的列。 The reason some columns do not have names is that the column size is variable and I did not assign a name for each column. 一些列没有名称的原因是该列的大小是可变的,并且我没有为每个列分配名称。

here's an example of the code, input file and output 这是代码,输入文件和输出的示例

dataframe = pandas.read_csv('filename.csv', sep = ",", header = 0)
dataframe = dataframe.iloc[::]
dataset = dataframe.values[:,0:]

input file 输入文件

A B C           
3 5 0      1    2   3

3   5   4      5    6   7

3   5   8      9    10  11

3   5   12     13   14  15

dataset output 数据集输出

dataset = [[1,2,3][5,6,7][9,10,11][13,14,15]]

How can I get dataframe to use the entire array (without the header)? 如何获取数据框以使用整个数组(不包含标题)?

I think you need .values to get back numpy ndarray. 我认为您需要.values来返回numpy ndarray。

from io import StringIO

csv_file = StringIO("""A B C
3 5 0 1 2 3
3 5 4 5 6 7
3 5 8 9 10 11
3 5 12 13 14 15""")

df = pd.read_csv(csv_file,sep='\s',engine='python')
df.values

Output: 输出:

array([[ 1,  2,  3],
       [ 5,  6,  7],
       [ 9, 10, 11],
       [13, 14, 15]])

为什么在加载csv文件时不跳过= 1

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

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