首页 > 解决方案 > Pandas read_excel 忽略了我需要保留的空白列

问题描述

stackoverflow 的长期用户,第一次发帖,感谢所有帮助:)

TLDR; 是否有我缺少的 read_excel 参数来保留所有列(即不删除任何列,即使它们是空白的)?

详细问题:当使用 read_excel 时,我有一个空白列作为我正在阅读的一张工作表中的第一列。然后从数据框中删除该空白列,这反过来又弄乱了其余代码,因为它依赖在工作表之间的列索引相同。由于其他表格遵循这种标准化格式,我无法围绕它进行编码。下面的代码是正确的。在空白列中添加填充值时,代码有效。由于将列索引 0(列 A)中的空白值设置为某个值所需的逻辑,这不是解决方案。

Pandas.read_excel 文档

import pandas as pd

df = pd.read_excel(
   "test.xlsx,
   sheet_name = "MYSHEET",
   # Missing parameter that i cannot figure out
)

# Code following this is looping row by row, column by column of each item in dataframe to get desired output

输入 excel 文件有双标题,A、B、C 列被转为标题值(因此我需要保留空白列)

标签: pythonexcelpandasdataframe

解决方案


似乎您可以使用usecols

df = pd.read_excel(
   "test.xlsx",
   sheet_name="MYSHEET",
   usecols="A:MV"
)

推荐阅读