Pandas中一共有三种数据结构,分别为:Series、DataFrame和MultiIndex(老版本中叫Panel )。
其中Series是一维数据结构,DataFrame是二维的表格型数据结构,MultiIndex是三维的数据结构。
Series
Series是一个类似于一维数组的数据结构,它能够保存任何类型的数据,比如整数、字符串、浮点数等,主要由一组数据和与之相关的索引两部分构成。
Series的创建
复制代码
1
2
3
4# 导入pandas import pandas as pd pd.Series(data=None, index=None, dtype=None)
- 参数:
- data:传入的数据,可以是ndarray、list等
- index:索引,必须是唯一的,且与数据的长度相等。如果没有传入索引参数,则默认会自动创建一个从0-N的整数索引。
- dtype:数据的类型
通过已有数据创建
复制代码
1
2
3
4
5
6
7
8
9
10#指定索引 pd.Series([6.7,5.6,3,10,2], index=[1,2,3,4,5]) ''' 1 6.7 2 5.6 3 3.0 4 10.0 5 2.0 dtype: float64 '''
复制代码
1
2
3
4
5
6
7color_count = pd.Series({'red':100, 'blue':200, 'green': 500, 'yellow':1000}) color_count red 100 blue 200 green 500 yellow 1000 dtype: int64
复制代码
1
2
3
4
5
6
7
8
9
10
11
12pd.Series(np.arange(10)) #一个参数时,参数值为终点,起点取默认值0,步长取默认值1。 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 dtype: int32
Series的属性
为了更方便地操作Series对象中的索引和数据,Series中提供了两个属性index和values
- index
复制代码
1
2
3
4
5color_count.index # 结果 Index(['blue', 'green', 'red', 'yellow'], dtype='object')
- values
复制代码
1
2
3
4color_count.values # 结果 array([ 200, 500, 100, 1000])
复制代码
1
2
3
4color_count[2] # 结果 100
最后
以上就是狂野老师最近收集整理的关于Pandas的Series结构的全部内容,更多相关Pandas内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复