pandas 如何自动清除python中的变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35399647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear variables in python automatically?
提问by Luis Ramon Ramirez Rodriguez
I'm working with csv files in pandas, these files are too big to be loaded at once so what I want to do is load once at a time, process it and load the next, something like this:
我正在使用Pandas中的 csv 文件,这些文件太大而无法一次加载,所以我想要做的是一次加载一次,处理它并加载下一个,如下所示:
data_frame1 = pd.read_csv('first_file.csv')
#some processing
# clear the variable here to free memory
data_frame2 = pd.read_csv('second_file.csv')
# some processing
....
....
....
Now, I know that:
现在,我知道:
%reset_selective data_frame1
will kill the variable but it puts a dialog this dialog:
将杀死变量,但它会在此对话框中放置一个对话框:
Once deleted, variables cannot be recovered. Proceed (y/[n])?
And I've to accept it in order to continue, I want to make this automatically so I don;t have to be in the computer just to press 'y'
而且我必须接受它才能继续,我想自动进行此操作,因此我不必在计算机中按“y”
回答by jb326
There's really no need to delete the variable at all. Python has automatic garbage collection, so if you assign a new DataFrame object to the same variable as your old one, the old one will be deleted automatically.
根本不需要删除变量。Python 具有自动垃圾收集功能,因此如果您将新的 DataFrame 对象分配给与旧的相同的变量,则旧的将自动删除。
More specifically, variables in Python behave as references to objects, so as long as you don't create multiple variables pointing to the same DataFrame object, once you assign a new object to your variable, your old DataFrame object will no longer be referenced by any variable, and will therefore be removed by the garbage collector.
更具体地说,Python 中的变量表现为对对象的引用,因此只要您不创建多个指向同一个 DataFrame 对象的变量,一旦您将新对象分配给您的变量,您的旧 DataFrame 对象将不再被引用任何变量,因此将被垃圾收集器删除。
回答by Luke Taylor
Better than setting x = None
is del x
, which will remove its reference from memory entirely like it was never set, and automatically mark the object for garbage collection if it has no other references. See this:
比设置更好的x = None
是del x
,它将完全从内存中删除它的引用,就像它从未设置过一样,如果没有其他引用,它会自动标记对象进行垃圾回收。看到这个:
>>> x = 5
>>> print x
5
>>> x = None
>>> print x
None
>>> del x
>>> print x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
回答by pp_
None
can be used to represent the absence of a value:
None
可用于表示没有值:
data_frame1 = None
Note: If you assign a new value to data_frame1
, Python will automatically take care of the memory deallocation anyway, so this probably won't be necessary.
注意:如果为 分配一个新值data_frame1
,Python 无论如何都会自动处理内存释放,因此这可能没有必要。
回答by Mike
The answer is to use the following command:
答案是使用以下命令:
%reset_selective -f data_frame1