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')