![](/img/trans.png)
[英]Plot a CSV file where the delimiter is '; ' (semicolon + space)
[英]Read CSV file with semicolon as delimiter
我有一个numpy
2D 数组,它的形状为(4898, )
,其中每行中的元素用分号分隔,但仍存储在单列而不是多列中(所需的结果)。 如何在二维数组的每个数组中每次出现分号时创建拆分。 我已经编写了以下 Python 脚本来执行此操作,但它会引发错误。
stochastic_gradient_descent_winequality.py
import numpy
import pandas
if __name__ == '__main__' :
with open('winequality-white.csv', 'r') as f_0 :
with open('winequality-white-updated.csv', 'w') as f_1 :
f_0.next()
for line in f_0 :
f_1.write(line)
wine_data = pandas.read_csv('winequality-white-updated.csv', sep = ',', header = None)
wine_data_ = wine_data
wine_data = numpy.array([x.split(';') for x in wine_data_], dtype = numpy.float)
print (numpy.shape(wine_data))
错误
Traceback (most recent call last):
File "stochastic_gradient_descent_winequality.py", line 16, in <module>
wine_data = numpy.array([x.split(';') for x in wine_data_], dtype = numpy.float)
AttributeError: 'numpy.int64' object has no attribute 'split'
如果您使用分号 ( ;
) 作为 csv 文件分隔符而不是逗号 ( ,
),则可以调整第一行:
wine_data = pandas.read_csv('winequality-white-updated.csv', sep = ';', header = None)
您的列表理解的问题在于[x.split(';') for x in wine_data_]
迭代列名称。
在这种情况下,您不需要使用列表理解的行。 您可以读入您的数据并完成。
wine_data = pandas.read_csv('winequality-white-updated.csv', sep = ',', header = None)
print (numpy.shape(wine_data))
假设你的 csv 文件是这样的:
2.12;5.12;3.12
3.1233;4;2
4;4.9696;3
2;5.0344;3
3.59595;4;2
4;4;3.59595
...
然后像这样更改您的代码:
import pandas, numpy
wine_data = pandas.read_csv('test.csv', sep = ',', header = None)
wine_data_ = wine_data
wine_data = numpy.array([x.split(';') for x in wine_data_[0]], dtype = numpy.float)
wine_data
wine_data
将是:
array([[ 2.12 , 5.12 , 3.12 ],
[ 3.1233 , 4. , 2. ],
[ 4. , 4.9696 , 3. ],
[ 2. , 5.0344 , 3. ],
[ 3.59595, 4. , 2. ],
[ 4. , 4. , 3.59595]])
提高效率:
import pandas, numpy
wine_data = pandas.read_csv('test.csv', sep = ';', header = None)
wine_data = numpy.array(wine_data,dtype = numpy.float)
在这
x.split(';') for x in wine_data_
无论你得到什么x
都不是字符串。 只有字符串有split()
。 如果它不是字符串,那么它会给出这个错误
对象没有属性 'split
检查您的x
值。
你可以试试这样的...
def get_y(r):
return str(r['label']).split(' ')
result :
(PILImage mode=RGB size=800x800, TensorMultiCategory([0., 0., 0., 1., 0., 0.]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.