Search code examples
pandasdrop

same Columns go missing in non related DF in pandas when use Drop


Very baffling to me.... when I drop cols 'Focus2','Score2','Focus3','Score3' from dfocus1, it drops the same cols in dfocus2

Why ?

'''

dfocus1=df
dfocus2=df
dfocus3=df

print('\nTable data',dfocus2.info(memory_usage='deep'))

dfocus1.drop(['Focus2','Score2','Focus3','Score3'], axis=1,inplace=True)

print('\nTable data',dfocus2.info(memory_usage='deep'))
'''

Solution

  • This is because dfocus1 and dfocus2 are pointing to the same object(df).

    Create a copy of df and then drop the columns:

    dfocus1=df.copy()
    dfocus2=df.copy()