小编Gau*_*rav的帖子

无法使用这些索引器对 <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']
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

不能做切片索引对'<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)
Run Code Online (Sandbox Code Playgroud)

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

python-3.x pandas

5
推荐指数
1
解决办法
3万
查看次数

标签 统计

pandas ×1

python-3.x ×1