Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop previous pandas tables after merged into 1

I want to merge two dataframes together and then delete the first one to create space in RAM.

df1 = pd.read_csv(filepath, index_col='False')
df2 = pd.read_csv(filepath, index_col='False')
df3 = pd.read_csv(filepath, index_col='False')
df4 = pd.read_csv(filepath, index_col='False')


result = df1.merge(df2, on='column1', how='left', left_index='True', copy='False')

result2 = result.merge(df3, on='column1', how='left', left_index='True', copy='False')

Ideally what I would like to do after this is delete all of df1, df2, df3 and have the result2 dataframe left.

like image 750
Aran Freel Avatar asked Oct 22 '25 16:10

Aran Freel


2 Answers

It's better NOT to produce unnecessary DFs:

file_list = glob.glob('/path/to/file_mask*.csv')

df = pd.read_csv(file_list[0], index_col='False')

for f in file_list[1:]:
    df = df.merge(pd.read_csv(f, index_col='False'), on='column1', how='left')

PS IMO you can't (at least shouldn't) mix up on and left_index parameters. Maybe you meant right_on and left_index - that would be OK

like image 66
MaxU - stop WAR against UA Avatar answered Oct 25 '25 05:10

MaxU - stop WAR against UA


Just use del

del df1, df2, df3, df4
like image 39
Mihkorz Avatar answered Oct 25 '25 07:10

Mihkorz