python pandas没有从csv文件中读取第一列

时间:2023-01-04 23:45:41

I have a simple 2 column csv file called st1.csv:

我有一个名为st1.csv的简单2列csv文件:

GRID    St1  
1457    614  
1458    657  
1459    679  
1460    732  
1461    754  
1462    811  
1463    748  

However, when I try to read the csv file, the first column is not loaded:

但是,当我尝试读取csv文件时,未加载第一列:

a = pandas.DataFrame.from_csv('st1.csv')  
a.columns

outputs:

输出:

 Index([u'ST1'], dtype=object)

Why is the first column not being read?

为什么没有读取第一列?

2 个解决方案

#1


36  

Judging by your data it looks like the delimiter you're using is a .

根据您的数据判断,您使用的分隔符看起来像是。

Try the following:

请尝试以下方法:

a = pandas.DataFrame.from_csv('st1.csv', sep=' ')

The other issue is that it's assuming your first column is an index, which we can also disable:

另一个问题是它假设你的第一列是一个索引,我们也可以禁用它:

a = pandas.DataFrame.from_csv('st1.csv', index_col=None)

#2


6  

Based on documentation which compares read_csv and from_csv, it shows that it is possible to put index_col = None. I tried the below and it worked:

根据比较read_csv和from_csv的文档,它显示可以放入index_col = None。我尝试了下面的工作:

DataFrame.from_csv('st1.csv', index_col=None);

This assumes that the data is comma-separated.

这假定数据以逗号分隔。

Please check the below link

请检查以下链接

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_csv.html

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_csv.html

#1


36  

Judging by your data it looks like the delimiter you're using is a .

根据您的数据判断,您使用的分隔符看起来像是。

Try the following:

请尝试以下方法:

a = pandas.DataFrame.from_csv('st1.csv', sep=' ')

The other issue is that it's assuming your first column is an index, which we can also disable:

另一个问题是它假设你的第一列是一个索引,我们也可以禁用它:

a = pandas.DataFrame.from_csv('st1.csv', index_col=None)

#2


6  

Based on documentation which compares read_csv and from_csv, it shows that it is possible to put index_col = None. I tried the below and it worked:

根据比较read_csv和from_csv的文档,它显示可以放入index_col = None。我尝试了下面的工作:

DataFrame.from_csv('st1.csv', index_col=None);

This assumes that the data is comma-separated.

这假定数据以逗号分隔。

Please check the below link

请检查以下链接

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_csv.html

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_csv.html