我已经构建了一个示例代码,以查看是否一切如我所愿。
predictorgroups=predictor.groupby("sector")
targetco=target.iloc[1]
group=predictorgroups.get_group(targetco.sector).astype(object)
pdf=(group).sort('size',ascending=False)[:10].astype(object)
它运行良好。所以我想升级并使该代码多次交互,所以我将其调整为:
for i in range(len(target.index[:10])):
predictorgroups=predictor.groupby("sector")
targetco=target.iloc(i)
group=predictorgroups.get_group(targetco.sector).astype(object)
pdf=(group).sort('size',ascending=False)[:10].astype(object)
请注意,此代码与上面的代码完全相同。但是在第二个中我收到了这个错误:
Traceback (most recent call last):
File "/file.py", line 64, in <module>
group=predictorgroups.get_group(targetco.sector).astype(object)
AttributeError: '_iLocIndexer' object has no attribute 'sector'
我注意到在第一个代码中,如果我打印 targetco
,我会得到一个只有一个索引的 Pandas 系列。如果我在第二个中做同样的事情,我会得到以下对象类型:
targetco pandas.core.indexing._iLocIndexer 对象位于 0x105a3e438
谁能给我解释一下为什么会这样?如果第二个代码相同,为什么我在第二个代码中得到不同的响应?
最佳答案
我觉得有错别字,把()
改成[]
:
targetco=target.iloc(i)
到
targetco=target.iloc[i]
因为:
targetco = target.iloc(1)
print (targetco)
<pandas.core.indexing._iLocIndexer object at 0x000000000A8CD358>
然后 pandas.core.indexing._iLocIndexer
对象没有列 sector
,因此引发错误:
print (targetco.sector)
AttributeError: '_iLocIndexer' object has no attribute 'sector'
关于python - Pandas - AttributeError : '_iLocIndexer' object has no attribute 'sector' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44621674/