Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

python pandas not reading first column from csv file

I have a simple 2 column csv file called st1.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:

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

outputs:

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

Why is the first column not being read?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

UPDATE:

In newer pandas versions, do:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...