使用Pandas读取Excel文件返回空DataFrame问题求助
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:
- Pandas found two columns (with those float values as names) but zero rows of data under them.
- 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
Openfile3.xlsxand 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 theheader=Noneparameter. 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_excelloads the first worksheet in the file. If your data is in a different sheet, use thesheet_nameparameter: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, useskiprowsto 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, useusecols(to pick columns) ornrows(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




