Pandas 未记录的 DataFrame.keys() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14718643/
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
Pandas undocumented DataFrame.keys() method
提问by Gill Bates
回答by abarnert
You can see where this is defined in the source:
您可以在源代码中看到它的定义位置:
def keys(self):
return self.columns
And, if you look at the git blame, you can see it was added as a fix for #1240: "Request: keys() method on dataFrame". The rationale seems to be:
而且,如果您查看 git blame ,您可以看到它是作为对#1240: "Request: keys() method on dataFrame"的修复而添加的。理由似乎是:
While learning Pandas this kind of method is useful to move from the well-understood dict structure to the more powerful DataFrame. As a pandas novice this kind of mental mapping would be much appreciated.
在学习 Pandas 时,这种方法对于从易于理解的 dict 结构转移到更强大的 DataFrame 很有用。作为Pandas新手,这种心理映射将不胜感激。
However, it's worth noting that DataFramesupports only about half of the mapping interface. For example, there's iteritemsand keys, but no iterkeys. And there are also cases where they added similar but not-quite-the-same names, like iterkv, which is equivalent to iteritemsbut there specifically because the latter "gets incorrectly converted to .items() by 2to3".
但是,值得注意的是,DataFrame仅支持大约一半的映射接口。例如,有iteritems和keys,但没有iterkeys。并且在某些情况下,他们添加了相似但不完全相同的名称,例如iterkv,这等效于iteritems但特别是因为后者“被 2to3 错误地转换为 .items()”。
You can go through the source and see where each of these were added and why, but there doesn't seem to be too much rhyme or reason beyond "DataFrameis kind of like a dict, and kind of not."
您可以查看源代码并查看添加了每个元素的位置以及添加原因,但似乎没有太多押韵或理由超出“DataFrame有点像dict,又不太像”。
The fact that they chose not to document most of these methods, or to document that DataFrameis kind of like a dict, I wouldn't rely on any of this. Just use columnsinstead of keys(), etc.
事实上,他们选择不记录大多数这些方法,或者记录DataFrame有点像 a的事实dict,我不会依赖其中的任何一个。只需使用columns代替keys()等。

