Does anyome know why I am receiving the following error?: AttributeError: 'numpy.float64' object has no attribute 'index'

Does anyome know why I am receiving the following error?: AttributeError: 'numpy.float64' object has no attribute 'index'

大家好,我收到了这个错误:

AttributeError: 'numpy.float64' object has no attribute 'index'

追溯看起来像这样:

AttributeError  Traceback (most recent call last)
<ipython-input-50-dfcbcabe20ea> in <module>()
      2  for name, df in all_data.items():
      3  top_10 = df.mean().dropna().sort_values().iloc[-10]
----> 4  top_10_columns[name] = top_10.index

而运行下面的代码:

top_10_columns = {}
for name, df in all_data.items():
    top_10 = df.mean().dropna().sort_values().iloc[-10]
    top_10_columns[name] = top_10.index

当您执行 .iloc[-10] 时,您意外地没有获得“前 10 个”项目,而是倒数第 10 个项目。所以 top_10numpy.float64 类型的单个值。给 iloc 一个范围应该可以解决它。 .iloc[0:10].iloc[-10:] 取决于您的排序是升序还是降序,并且您想要获得前十项 (.iloc[0:10]) 或后十项 (.iloc[-10:])。

您正在尝试分配给数组,但 Python 将 top_10_columns 解释为浮点数。在你的 for 循环之上,你必须将它声明为一个数组,即 top_10_columns = []