[英]Python Pandas read_csv not importing correctly
I have a .xls
file that looks similar to this... 我有一个.xls
文件,看起来与此类似...
Value of Construction Put in Place...
(Millions of Dollars....)
Blank Row
Date Total_Construction Total Residential Total Nonresidential...Columns
Dec-15 1,116,570 435,454 681,217
Nov-15 1,115,966 432,295 683,671
Oct-15 1,122,749 431,164 691,585
.
.
.
I am trying to import the file to get the following: 我正在尝试导入文件以获取以下信息:
Date Total_Construction Total Residential Total Nonresidential
Dec-15 1,116,570 435,454 681,217
Nov-15 1,115,966 432,295 683,671
Oct-15 1,122,749 431,164 691,585
.
.
.
With the following code: 使用以下代码:
for chunk in pandas.read_csv('/PATH/totsatime.xls',
names = ['Date', 'Total Residential', 'Total Nonresidential'],
header = 4,
chunksize = 1,
skiprows = range(1, 4),
thousands = ','):
if chunk['Date'] == 'Dec-01':
break
else:
df = pandas.DataFrame(chunk)
However, I end up with the following: 但是,我最终得到以下结果:
Date Total Residential Total Nonresidential
Lodging NaN NaN
Office NaN NaN
Commercial NaN NaN
Health care NaN NaN
The dates end up being formatted from the columns that I am not importing. 日期最终从我不导入的列中格式化。 Any advice would greatly be appreciated. 任何建议将不胜感激。
Thank you in advance. 先感谢您。
Don't use read_csv
to import an xls file. 不要使用read_csv
导入xls文件。 Use read_excel
. 使用read_excel
。 See http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html 参见http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.