【发布时间】:2017-07-24 19:54:23
【问题描述】:
我有一些凌乱的 Excel 电子表格,想将少量单元格导入数据框中。包含我不感兴趣的信息 (".") 和带有空格的单元格 (~) 的行数和列数因电子表格而异。
这是一个电子表格示例(输入):
~ ~ ~ ~
. . . . .
~ . . .
. . . . .
~ ~ ~
~ Name ID Description Notes
12 a AA aA None
3 b BB bB sentence one
44 c CC cC None
9 d DD dD Int
我的逻辑是否正确,我需要先将完整的电子表格导入数据帧,然后将该数据帧解析为唯一字符串 (Name) 作为标题行到第二个要使用的数据帧中?
这就是我导入的方式,但在解析到第二个数据帧时卡住了:
import pandas
file = 'example.xlsx'
xl = pandas.ExcelFile(file, dtype=str) #some cells have values and want just as strings
df = xl.parse("Sheet1")
for index, row in df.iterrows():
for cell in row:
if 'Name' in str(cell):
header_start_row = index
break
所需的熊猫数据框(输出):
Name ID Description Notes
a AA aA None
b BB bB sentence one
c CC cC None
d DD dD Int
【问题讨论】: