【问题标题】:Cannot identify what this "2" is in pandas dataframe header?无法识别熊猫数据帧标头中的“2”是什么?
【发布时间】: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 读取文件,如果是,您是否尝试使用 skiprowsheader
  • 否则,new_header = df.iloc[2].values

标签: python pandas dataframe numpy


【解决方案1】:

当您拉出第三行 df.iloc[2] 时,检查您的new_header 的结果。您会注意到它在输出中有索引2。这就是它的来源。您可以通过将第一行更改为 new_header = df.iloc[2].to_list() 来摆脱它

【讨论】:

  • 我之前尝试检查 new_header 输出,但没有意识到它被命名为 '2' 并且只是检查输出值。解决了,谢谢!
  • 去过了,不客气! :)
猜你喜欢
  • 2017-10-20
  • 2015-05-28
  • 1970-01-01
  • 2021-01-05
  • 2017-01-11
  • 2021-08-14
  • 2018-04-28
  • 1970-01-01
相关资源
最近更新 更多