无法使用这些索引器对 <class 'pandas.core.indexes.period.PeriodIndex'> 进行切片索引

新手上路,请多包涵
file_location3 = "F:/python/course1_downloads/City_Zhvi_AllHomes.csv"

housing = pd.read_csv(file_location3)
housing.set_index(['State','RegionName'],inplace=True)
housing = housing.iloc[:, 49:]

housing = housing.groupby(pd.PeriodIndex(housing.columns,freq='Q'),axis=1).mean()

data = housing
data = data.iloc[:,'2008q3' : '2009q2']

我得到的错误是:

无法使用这些索引器对 '<class 'pandas.core.indexes.period.PeriodIndex'> 进行切片索引 --- [2008q3] <'class 'str'>

现在我收到另一个错误

def price_ratio(row):
    return (row['2008q3'] - row['2009q2']) / row['2008q3']

data['up&down'] = data.apply(price_ratio, axis=1)

这给我错误: KeyError: ('2008q3', 'occurred at index 0')

原文由 Gaurav 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 362
1 个回答

尝试:

 data.loc[:,'2008q3':'2009q2']

原文由 Scott Boston 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进