pandas 为什么将熊猫导入为 pd 是惯例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35697404/
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
Why it's a convention to import pandas as pd?
提问by Juan
I see that in the pandas documentation they recommend importing pandas as:
我在Pandas文档中看到他们建议将Pandas导入为:
import pandas as pd
I can see some sense in doing that for when you are using pandas in an interactive context (as with a ipython/jupyter notebook), but I've seen it in production code and in widespread libraries (like Bokeh: https://github.com/bokeh/bokeh/search?p=2&q=pd&type=Code&utf8=%E2%9C%93). Is there a reason for that apart from convention?
当您在交互式上下文中使用 Pandas(如 ipython/jupyter 笔记本)时,我可以看到这样做的意义,但我已经在生产代码和广泛使用的库中看到了它(如 Bokeh:https://github .com/bokeh/bokeh/search?p=2&q=pd&type=Code&utf8=%E2%9C%93)。除了约定之外还有其他原因吗?
回答by
Because there are built-in
methods in python which overlap with the pandas methods. Like map(), all(), any(), filter(), max(), min()
and many others. In order to avoid the confusion that these methods used are from pandas or built-in. It is always better to import pandas as import pandas as pd
and call the pandas methods using the pd
prefix.
因为built-in
python中有一些方法与pandas方法重叠。像map(), all(), any(), filter(), max(), min()
和许多其他人一样。为了避免混淆使用的这些方法是来自pandas还是内置的。导入 pandas asimport pandas as pd
并使用pd
前缀调用 pandas 方法总是更好。
There might be other libraries which have the same method names, so to avoid overriding
we use the prefix part.
可能还有其他库具有相同的方法名称,因此为了避免overriding
使用前缀部分。