问题解决: Pandas: KeyError: […] not in index
报错代码:df_trainset=df[['time','temp','hum','cooling']]报错说index里没有这些(主要df也是拼接来的,如果是文件读出的表是可以这么取数据的。)猜测跟pandas内部有关系,索引的地址不一样。于是试了一下这个解决方案:df=df.copy()df_trainset=df[['time','temp','hum','cooling']]果然就可以了。
·
报错代码:
df_trainset=df[['time','temp','hum','cooling']]
报错说index里没有这些(主要df也是拼接来的,如果是文件读出的表是可以这么取数据的。)
猜测跟pandas内部有关系,索引的地址不一样。
于是试了一下这个解决方案:
df=df.copy()
df_trainset=df[['time','temp','hum','cooling']]
果然就可以了。(我可真棒啊~)
附另一版解决方案:
因为这几个特征列在原表里是不连续的,而且数量不多,所以采用Series拼接的方式解决
代码如下:
s1=df['time']
s2=df['temp']
s3=df['hum']
s4=df['cooling']
df_trainset=pd.concat([s1,s2,s3,s4],axis=1) #横向拼接
print(df_trainset)
顺利解决
更多推荐
已为社区贡献1条内容
所有评论(0)