You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用Pandas读取Excel文件返回空DataFrame问题求助

Empty DataFrame with odd column names when reading Excel via pandas

Hey there, let's break down why you're seeing that empty DataFrame with those weird float column names, and how to fix it.

First, let's recap what you're working with:
Your code:

import pandas as pd
x = pd.read_excel('file3.xlsx')
print(x)

And the output you got:

Empty DataFrame
Columns: [-114.31190317508207, -115.47192812345514 ]
[0 rows x 2 columns]

What's going on here?

That output tells us two key things:

  1. Pandas found two columns (with those float values as names) but zero rows of data under them.
  2. Those float column names mean pandas is treating the first row of your Excel file as the header row—but there's no actual data rows below it.

Common fixes to try:

  • Check your Excel file directly
    Open file3.xlsx and verify:

    • Is there actual data below those float values? If those floats are supposed to be data (not headers), then pandas misread the file structure.
    • Are there empty rows/columns before your data? Sometimes extra blank space can throw off pandas' auto-detection.
  • Force pandas to ignore headers (if your data has no column names)
    If those float values are supposed to be the first row of data (not column headers), use the header=None parameter. This tells pandas to generate default column names (0,1,2...) and read all rows as data:

    x = pd.read_excel('file3.xlsx', header=None)
    
  • Specify the correct worksheet
    By default, read_excel loads the first worksheet in the file. If your data is in a different sheet, use the sheet_name parameter:

    x = pd.read_excel('file3.xlsx', sheet_name='YourSheetName')
    
  • Skip empty leading rows
    If there are blank rows at the top of your Excel file, use skiprows to jump over them. For example, to skip the first 2 rows:

    x = pd.read_excel('file3.xlsx', skiprows=2)
    
  • Define exactly which columns/rows to read
    If you know the range of your data, use usecols (to pick columns) or nrows (to limit rows) to narrow down what pandas loads. For example:

    # Read only columns A and B, up to 10 rows
    x = pd.read_excel('file3.xlsx', usecols='A:B', nrows=10)
    

Quick test to confirm:

Try running this to get a detailed view of what pandas is detecting in your Excel file:

from pandas.io.excel import ExcelFile
with ExcelFile('file3.xlsx') as xls:
    print(xls.sheet_names)  # List all sheets
    df = pd.read_excel(xls, header=None)
    print(df.head(10))  # Show first 10 rows (including any weird headers)

This will help you see exactly what's in the file and where the data (or lack thereof) is.

内容的提问来源于stack exchange,提问作者Ali Shan

火山引擎 最新活动