3
這是非常奇怪的錯誤在我的腦海不能做切片索引上的<class 'pandas.indexes.range.RangeIndex'>這些索引
import numpy as np
import pandas as pd
df = pd.DataFrame({'head': [1, 1,2,2,1,3,2,3,1,1,1,2, 3,3],
'appraisal': [1,2,1,3,1,4,1,5,1,1,2,3,4,5]})
然後
df.loc[df.head, 'appraisal'].mean()
和
TypeError: cannot do slice indexing on <class 'pandas.indexes.range.RangeIndex'> with these indexers
但是,如果我改變'頭'爲充足的'head_id'它的工作是正確的
df = pd.DataFrame({'head_id': [1, 1,2,2,1,3,2,3,1,1,1,2, 3,3],
'appraisal': [1,2,1,3,1,4,1,5,1,1,2,3,4,5]})
df.loc[df.head_id, 'appraisal'].mean()
2.0
有什麼不對?
是的,但是'頭'有什麼問題,並用'head_id'糾正? – Edward
,因爲'head_id'不是熊貓的功能,所以它完美的工作。 – jezrael