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
191 views
in Technique[技术] by (71.8m points)

python - How do I read a fixed width format text file in pandas?

I just got my hands on pandas and am figuring out how I can read a file. The file is from the WRDS database and is the SP500?constituents?list all the way back to the 1960s. I checked the file and no matter what I do to import it using read_csv, I still can't display the data correctly.

df = read_csv('sp500-sb.txt')

df

<class 'pandas.core.frame.DataFrame'>
Int64Index: 1231 entries, 0 to 1230
Data columns: gvkeyx ? ? ?from ? ? ?thru ? ? conm
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? gvkey ? ? ?co_conm
...(the column names)
dtypes: object(1)

What does the above chunk of output mean? Anything would be helpful.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

pandas.read_fwf() was added in pandas 0.7.3 (April 2012) to handle fixed-width files.

  1. API reference

  2. An example from other question


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

...