我想使用 python pandas 读取 .xlsx。问题是在 excel 文件的开头,它有一些额外的数据,如表格的标题或描述,表格内容开始。这引入了未命名的列,因为 Pandas DataReader 将它作为列。但是表格内容在几行之后开始。
A B C
this is description
last updated: Mar 18th,2014
Table content
Country Year Product_output
Canada 2017 3002
Bulgaria 2016 2201
...
表内容从第 4 行开始。列必须是“国家”、“年份”、“proudct_output”而不是“这是描述”、“未命名”、“未命名”。对于这种特定情况,将skiprows
参数设置为 3 解决了问题(来自 Mikhail Venkov)。但是我要处理很多excel文件,我不知道要提前跳过多少行。我认为可能有一个解决方案,因为每个表列标题都有一个过滤器。
如果您知道特定文本(如国家/地区)必须在第一列中,您可以执行以下操作:
import xlrd
xl_work = xlrd.open_workbook("Classeur1.xlsx")
mySheet = xl_work.sheet_by_index(0)
nl = 0
while mySheet.cell_value(nl,0) != "Country" :
nl += 1
line_with_headers = nl
然后使用跳过nl
而不是 3。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句