首页 > 解决方案 > 熊猫专栏没有永久改变

问题描述

这是我的代码有问题。在双 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)

标签: pandas

解决方案


你必须inplace=True在你的代码中添加df = pd.concat([df, tmp_df], axis=1)


推荐阅读