gpt4 book ai didi

python-3.x - 无法使用这些索引器对 进行切片索引

转载 作者:行者123 更新时间:2023-12-01 02:43:10 26 4
gpt4 key购买 nike

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

我得到的错误是:

cannot do slice indexing on '<class
'pandas.core.indexes.period.PeriodIndex'>
with these indexers [2008q3] of <'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')

最佳答案

尝试:

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

关于python-3.x - 无法使用这些索引器对 <class 'pandas.core.indexes.period.PeriodIndex' > 进行切片索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49281713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com