简体   繁体   English

pandas.read_excel为列导入错误的值

[英]pandas.read_excel imports wrong values for a column

I am using pandas.read_excel to import an excel file into a DataFrame. 我正在使用pandas.read_excel将Excel文件导入到DataFrame中。 This is the Code... 这是代码...

#!/usr/bin/python
import pandas as pd
file = 'sample.xls'
df = pd.read_excel(file, sheetname=0, skiprows=7)

This imports the file but with the below warning... 这将导入文件,但带有以下警告...

WARNING *** OLE2 stream 'SSCS': expected size 128640, actual size 512 警告*** OLE2流“ SSCS”:预期大小128640,实际大小512

And When when I print the dataframe, I see that the last column has completely wrong values(instead of actual values from that column, it has shows 4 for every row. 当我打印数据框时,我看到最后一列的值完全错误(而不是该列的实际值,每行显示4)。

I am attaching a screen grab of the xls file. 我正在附上xls文件的屏幕抓图。 示例Xls文件第一页屏幕抓取

If you are using Windows, you could use Excel itself to modify all of the XLS files before loading them with Pandas. 如果使用Windows,则可以在使用Pandas加载之前,使用Excel本身修改所有XLS文件。 The following script will automatically unhide all of the columns in all XLS files found in a given folder: 以下脚本将自动取消隐藏在给定文件夹中找到的所有XLS文件中的所有列:

import win32com.client as win32
import glob

excel = win32.gencache.EnsureDispatch('Excel.Application')

for xls in glob.glob(r"C:\My Path\*.xls"):
    print xls
    wb = excel.Workbooks.Open(xls)
    ws = wb.Worksheets(1)
    ws.Columns.EntireColumn.Hidden = False
    excel.DisplayAlerts = False     # Allow file overwrite
    wb.Close(True)

excel.Application.Quit()

You might want to make a copy of your XLS files before doing this as it will be done in place. 您可能需要先复制XLS文件,因为它会就地完成。 Alternatively, you could use wb.SaveAs() to specify a different output location. 另外,您可以使用wb.SaveAs()指定其他输出位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM