[英]read strings from .csv file in python
I am reading a .csv file using pandas, this is my code: 我正在使用熊猫读取.csv文件,这是我的代码:
import pandas as pd
df=pd.read_csv('MyFile.csv','r')
numeric_col=df.ix[:,0] #numeric values, works fine
string_col=df.ix[:,1] #string values, equals to nan
Does anyone know why I am not able to read the string column? 有谁知道我为什么无法阅读字符串列?
(or to be more accurate: I am able to read some string columns, but not others. For example, this is the first line of the csv: (或更准确地说:我能够读取一些字符串列,但不能读取其他字符串列。例如,这是csv的第一行:
20150329,3002,1,20000,32459,5100,10251181,DEADFALL,RAA,S,10251181,0
I am able to read col. 我能够阅读col。 7 (
'DEADFALL'
) but not col. 7(
'DEADFALL'
),而不是col。 8 ( RAA
)). 8(
RAA
))。
You can try to read the file in this way: 您可以尝试以这种方式读取文件:
f = pd.read_csv('MyFile.csv',header=None)
Since it seems that your file has no header line. 由于您的文件似乎没有标题行。 Your file should look then like this when reading:
阅读时,文件应如下所示:
0 1 2 3 4 5 6 7 8 9 \
0 20150329 3002 1 20000 32459 5100 10251181 DEADFALL RAA S
10 11
0 10251181 0
Then you can access the single column by: 然后,您可以通过以下方式访问单列:
str_col = df[8]
or you can later rename the columns with different headers passing a list of headers string, for instance: 或者您以后可以用不同的标题来重命名这些列,并传递一个标题字符串列表,例如:
f.columns = [list_of_strings]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.