熊猫 - inplace,view,copy confusion

时间:2017-05-08 20:24:58

标签: python pandas dataframe

我遇到了Pandas数据帧的问题。 似乎Pandas / Python在我的代码中的某处生成了DF的副本,而不是对原始DF执行修改。

在下面的代码中,“update_df”仍然看到带有“file_exists”列的DF,该列应该已被前一个函数删除。

MAIN:

if __name__ == '__main__':
    df_main = load_df()
    clean_df2(df_main)
    update_df(df_main, image_path_main)
    .....

clean_df2

def clean_df2(df): #remove non-existing files from DF
    df['file_exists'] = True # add column, set all to True?
    .....
    df = df[df['file_exists'] != False] #Keep only records that exist
    df.drop('file_exists', 1, inplace=True)  # delete the temporary column
    df.reset_index(drop=True, inplace = True)  # reindex if source has gaps

update_df:

def update_df(df, image_path): #add DF rows for files not yet in DF
    print(df)
    ....

1 个答案:

答案 0 :(得分:1)

我想你做的时候:

df = df[df['file_exists'] != False]

您已创建原始df的副本。

要使其正常工作,您可以将功能更改为:

def clean_df2(df): #remove non-existing files from DF
    df['file_exists'] = True # add column, set all to True?
    .....
    return df

当您调用clean_df2(df)时,请执行以下操作:

df = clean_df2(df)