我正在做一些事情,我想从创建它的另一个函数中删除发送到一个函数中的数据帧。我该怎么办?
示例:
获取df
并将其删除的功能:
def delete_file(df):
del df
用于创建df
并将其发送到我的delete_file()
函数的函数:
def create_and_delete_file():
import pandas as pd
# create / import dataframe
x = pd.DataFrame({'name':['jon','mary'],
'age':[12,45]})
# send the dataframe to my delete function
delete_file(x)
# now print it
print(x)
让我们调用函数。如您所见,将数据帧x
发送到我的delete_file
函数之后,我仍然可以打印它。
create_and_delete_file()
name age
0 jon 12
1 mary 45
如何删除它?
编辑(更多信息如下)(我知道有更好的方法来分析df,这只是假设)该数据框具有4个城市,均分。 Datafarme的大小为1GB。
# dataframe size 1 Gigabyte
def _calculate_city_sales(df):
'''
The dataframe 'df' is 1 GB in size
We will split it into four segments
Each segment is 250Mb in size
So at the end of the operation (Berlin)
has RAM usage not doubled?
'''
NY = df[df['city']=='NY']
sales = NY.sales.sum()
print(f'New York Sales: {sales}') # 1GB + 250Mb of RAM in use
LA = df[df['city']=='LA']
sales = LA.sales.sum()
print(f'Los Angeles Sales: {sales}') # 1GB + 250Mb + 250Mb of RAM in use
HK = df[df['city']=='HK']
sales = HK.sales.sum()
print(f'Hong Kong Sales: {sales}') # 1GB + 250Mb + 250Mb + 250Mb of RAM in use
BL = df[df['city']=='BL']
sales = BL.sales.sum()
print(f'Berlin Sales: {sales}') # <----- Ram Usage now at 2GB?
答案 0 :(得分:0)
尝试将df设为无。
def delete_file(df):
df = None
现在,如果您打印df,它应该显示“无”。我不知道为什么del不起作用。但是,您无需手动删除它,因为python在its own.
上将其删除