【发布时间】:2022-01-06 17:24:55
【问题描述】:
我正在编写一个脚本来格式化我经常使用的 Excel 工作表模板中的数据,这样我就可以使用它而不必每次都手动格式化它。我正在使用以下代码删除一些出现的无用标题行,并使第三行成为实际标题。
new_header = df.iloc[2] #grab the third row for the header
df = df[3:] #take the data below the new header row
df.columns = new_header #set the header row as the df header
df.reset_index(drop=True, inplace=True)
这很好用,除非当我查看数据框时,我的索引上方有一个 2。这似乎不是索引名称或列名称(我都检查过),并且似乎不存在多索引。这看起来很简单,但我对这个 2 是什么以及如何删除它感到困惑。
任何帮助将不胜感激。
【问题讨论】:
-
请提供您的输入数据框。
-
这可能是 XY 问题 - 您是否使用
read_excel读取文件,如果是,您是否尝试使用skiprows和header? -
否则,
new_header = df.iloc[2].values
标签: python pandas dataframe numpy