pandas Python中字典和pandas系列的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43635694/
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
Difference between dictionary and pandas series in Python
提问by avni hirpara
I have a requirement to keep data in key value pairs. I search and found 2 ways in python:
我需要将数据保存在键值对中。我在python中搜索并找到了两种方法:
default data structure dictionary.
x = {'key':value} value = x['key']
series of pandas data structure.
x = pandas.Series({'key':value}) value = x.key
默认数据结构字典。
x = {'key':value} value = x['key']
Pandas 数据结构系列。
x = pandas.Series({'key':value}) value = x.key
I want to know the difference between this two apart from syntax.
除了语法之外,我想知道这两者之间的区别。
回答by John Moutafis
Always read the docs first
But since you asked:
总是先阅读文档
但是既然你问了:
- Dictionariesare one of python's default data structures which
allow you to store
key: value
pairs and offer some built-in methods to manipulate your data, which you can read on the docs (here is a good summaryto jump start your reading process). - Panda's Seriesare one-dimensional ndarrayswith
axis-labels, which allow you to store
array-like, dict, or scalar
values and are one of numpy's (a scientific computing python library) built-in data structures.
If you read the docs provided above (see:Panda's Serieslink) you will notice that they come with a vast amount of methods and attributes quite different, for the most part, from those of a python dictionary.
- 字典是 python 的默认数据结构之一,它允许您存储
key: value
对并提供一些内置方法来操作您的数据,您可以在文档中阅读这些内容(这是一个很好的摘要,可以快速开始您的阅读过程)。 - Panda 的系列是带有轴标签的一维ndarray,它允许您存储
array-like, dict, or scalar
值,并且是numpy的(科学计算 Python 库)内置数据结构之一。
如果您阅读上面提供的文档(请参阅:Panda 的系列链接),您会注意到它们带有大量方法和属性,在大多数情况下,它们与 Python 字典的方法和属性大不相同。
So it is not just a syntax difference to say the least.
因此,至少可以说,这不仅仅是语法上的差异。
If you only need to store some key:value
pairs, your best and more elegant solution is to use the default dictionary. If you need to make some complex data manipulation on the stored data, then considerusing panda's series.
如果您只需要存储一些key:value
对,最好且更优雅的解决方案是使用默认字典。如果需要对存储的数据进行一些复杂的数据操作,那么可以考虑使用panda的系列。
回答by maxalmond
There are 2 important differences.
有两个重要的区别。
1) Syntax and associated methods Allows for complex data manipulation in Panda series that would be difficult to achieve using a standard dictionary.
1) 语法和相关方法允许在 Panda 系列中进行复杂的数据操作,这是使用标准字典难以实现的。
2) Order Standard python dictionaries are unordered sets; values can only be accessed by keys. Data in Panda series can be accessed by keys BUT can also be accessed with a numeric index because they are ordered.
2)Order 标准python字典是无序集;值只能通过键访问。Panda 系列中的数据可以通过键访问,但也可以通过数字索引访问,因为它们是有序的。
In some ways, Panda series combine the best worlds of standard lists and standard dictionaries in python, but then top it off with some great data manipulation methods.
在某些方面,Panda 系列结合了 Python 中标准列表和标准字典的最佳世界,但又以一些出色的数据操作方法来结束它。