有人知道我收到以下错误的原因吗?:AttributeError:'numpy.float64'对象没有属性'index'

2024-04-18 23:38:20 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,伙计们,我遇到了这个错误:

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

Tags: columnsnameindffordataindextop
2条回答

当您执行.iloc[-10]操作时,您无意中没有获得“前10名”项目,而只是倒数第10名。因此top_10numpy.float64类型的单个值。给iloc一个范围应该可以解决这个问题.iloc[0:10].iloc[-10:]取决于排序是升序还是降序,并且您希望获取前十项(.iloc[0:10])或后十项(.iloc[-10:]

您正试图分配给数组,但Python将top_10_列解释为浮点。在for循环上方,必须将其声明为数组,即top_10_columns=[]

相关问题 更多 >