简体   繁体   English

熊猫加载csv字符串浮动

[英]Panda load csv string to float

Yes, you won't believe it, but i've been browsing for two hours for a simple line of code. 是的,您不会相信,但是我已经浏览了两个小时的简单代码行。 HOW DO YOU convert DataFrame full of strings to float Dataframe. 您如何将充满字符串的DataFrame转换为float Dataframe。 Even more, how do you convert DataFrame full of string to np.array? 还有,如何将充满字符串的DataFrame转换为np.array? There seems to be two solutions which are being suggested over and over - convert objects and astype. 似乎一再提出了两种解决方案-转换对象和astype。 None of them work..... 他们都不工作.....

database = pd.read_csv('test1.csv',header=None)
database


Out[165]:
0
0   0,0,1,0,0
1   1,0,1,0,0
2   0,4,0,1,0
3   1,4,0,1,0
4   1,1,0,0,1
5   2,1,0,0,1

database = database.astype(str).convert_objects(convert_numeric=True)
x = np.array(database)

In [170]:
x
Out[170]:
array([['0,0,1,0,0'],
       ['1,0,1,0,0'],
       ['0,4,0,1,0'],
       ['1,4,0,1,0'],
       ['1,1,0,0,1'],
       ['2,1,0,0,1']], dtype=object)

OR 要么

DATA = database.astype(float)
ValueError: could not convert string to float: '2,1,0,0,1'

From what I've seen in your comment, this might help: 根据我在您的评论中看到的内容,这可能会有所帮助:

import pandas
pandas.read_csv('test1.csv', header=None, lineterminator=' ')

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

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