pandas - 熊猫专栏没有永久改变
问题描述
这是我的代码有问题。在双 for 循环中,我创建了一个临时数据框,它将添加到原始数据框中。在添加之前,我更改了列。但是当我检查最终数据框时,该列没有改变。
df['sd'] = labelencoder.fit_transform(df['sd'])
copy_columns_of_x = ['consumer_price_index', 'households', 'income', 'avg_price_jeonse', 'total_unsold_households_rate']
copy_columns_of_y = ['avg_price_meme']
df[copy_columns_of_x] = standard_scaler.fit_transform(df[copy_columns_of_x])
df_orgin = df.copy()
columns = copy_columns_of_x
for c in copy_columns_of_x:
for h in HISTORY_FRAME_TO_PREDICT:
tmp_df = df_orgin.sort_values(['sd','yyyymmdd']).groupby(['sd'])[c].transform(lambda x:x.shift(h))
changed_column = c + '_' + str(h) # todo : 이거 왜 안먹지?
tmp_df.columns = [changed_column]
df = pd.concat([df, tmp_df], axis=1)
解决方案
你必须inplace=True
在你的代码中添加df = pd.concat([df, tmp_df], axis=1)
推荐阅读
- node.js - 添加 NavLink 后如何修复“× TypeError: Cannot read property 'pathname' of undefined”?
- php - 如何将 json Web 令牌转换为 json 对象?
- python - 如何让 argparse 为具有两个名称的 python 脚本显示一个帮助页面?
- python - 使用宏插入更多宏
- php - 用axios发布后获取返回的数据(vue+laravel)
- python - 此 R 代码是否有 python 进程
- netlogo - 比较补丁
- python - Python Selenium time.sleep() 隐式工作_wait() 和 WebDriverWait 失败
- c - 按值返回结构每次都会给出相同的错误答案
- python - 如何在具有基于类的视图的 django 模板中使用模型方法