1、Series类型
-
Series类型是由一组数据及与之相关的数据索引组成。
![](https://img.haomeiwen.com/i13629684/4f2dcb2ec2ee723a.png)
>>> import pandas as pd
>>> a=pd.Series([1,2,3,4])
>>> a
0 1
1 2
2 3
3 4
dtype: int64
![](https://img.haomeiwen.com/i13629684/13e47722ccb4bedd.png)
>>> import pandas as pd
>>> b=pd.Series([5,6,7,8],index=['a','b','c','d'])
>>> b
a 5
b 6
c 7
d 8
dtype: int64
>>> c=pd.Series([9,0,1,2],['a','b','c','d'])
>>> c
a 9
b 0
c 1
d 2
dtype: int64
![](https://img.haomeiwen.com/i13629684/3873ec0a67cb709e.png)
2、Series类型创建
-
Series类型可以由如下类型创建:
-
标量值,index表达Series类型的尺寸
-
python列表,index与列表元素个数一致
-
python字典,,键值对中的“键”是索引,index从字典中进行选择操作
-
ndarray,索引和数据都可以通过ndarray类型创建
-
其他函数,range()函数等
-
-
从标量值创建
>>> import pandas as pd
>>> d=pd.Series(25,index=['a','b','c','d'])
>>> d
a 25
b 25
c 25
d 25
dtype: int64
>>> d=pd.Series(25)
>>> d
0 25
dtype: int64
-
从字典类型创建
>>> import pandas as pd
>>> e=pd.Series({'a':9,'b':2,'c':8})
>>> e
a 9
b 2
c 8
dtype: int64
>>> f=pd.Series({'a':9,'b':2,'c':8},index=['c','a','b','e'])
>>> f
c 8.0
a 9.0
b 2.0
e NaN
dtype: float64
-
从ndarray类型创建
>>> import numpy as np
>>> import pandas as pd
>>> n=pd.Series(np.arange(5))
>>> n
0 0
1 1
2 2
3 3
4 4
dtype: int32
>>> m=pd.Series(np.arange(5),index=np.arange(9,4,-1))
>>> m
9 0
8 1
7 2
6 3
5 4
dtype: int32
3、Series类型的基本操作
-
Series类型包括index和values两部分
-
Series类型的操作类似ndarray类型
-
Series类型的操作类似Python字典类型
>>> import pandas as pd
>>> b=pd.Series([1,2,3,4],['a','b','c','d'])
>>> b
a 1
b 2
c 3
d 4
dtype: int64
>>> b.values
array([1, 2, 3, 4], dtype=int64)
>>> b.index
Index(['a', 'b', 'c', 'd'], dtype='object')
>>> b['b']
2
>>> b[0]
1
>>> b[['a','d',0]]
Warning (from warnings module):
。。。。(省略)(注意:自动索引和自定义索引虽然并存但不能混用)
a 1.0
d 4.0
0 NaN
dtype: float64
>>> b[['c','b','a']]
c 3
b 2
a 1
dtype: int64
-
注意:自动索引和自定义索引虽然并存但不能混用
-
Series类型的操作类似ndarray类型:
• 索引方法相同,采用[]
• NumPy中运算和操作可用于Series类型
• 可以通过自定义索引的列表进行切片
• 可以通过自动索引进行切片,如果存在自定义索引,则一同被切片
>>> import numpy as np
>>> import pandas as pd
>>> a=pd.Series([1,2,3,4],['a','b','c','d'])
>>> a
a 1
b 2
c 3
d 4
dtype: int64
>>> a[2]
3
>>> a['c']
3
>>> a[:3]
a 1
b 2
c 3
dtype: int64
>>> a[:'c']
a 1
b 2
c 3
dtype: int64
>>> a[:'d']
a 1
b 2
c 3
d 4
dtype: int64
>>> a[a>a.median()] #中位数
c 3
d 4
dtype: int64
>>> a[a>a.mean()] #平均数
c 3
d 4
dtype: int64
>>> np.exp(a)
a 2.718282
b 7.389056
c 20.085537
d 54.598150
dtype: float64
-
Series类型的操作类似Python字典类型:
• 通过自定义索引访问
• 保留字in操作
• 使用.get()方法
>>> import pandas as pd
>>> a=pd.Series([1,2,3,4],['a','b','c','d'])
>>> a
a 1
b 2
c 3
d 4
dtype: int64
>>> a['a']
1
>>> 'c' in a
True
>>> a.get('b')
2
>>> a.get(1)
2
>>> a.get('e',5)
5
4、Series对齐操作
-
Series类型在运算中会自动对齐不同索引的数据
>>> import pandas as pd
>>> a=pd.Series([1,2,3,4],['a','b','c','d'])
>>> b=pd.Series({'c':10,'d':8,'e':12,'a':34})
>>> a+b
a 35.0
b NaN
c 13.0
d 12.0
e NaN
dtype: float64
5、Series类型的name属性
-
Series对象和索引都可以有一个名字,存储在属性.name中
>>> import pandas as pd
>>> a=pd.Series([1,2,3,4],['a','b','c','d'])
>>> a.name='Series 对象'
>>> a.index.name='索引'
>>> a
索引
a 1
b 2
c 3
d 4
Name: Series 对象, dtype: int64
6、Series类型的修改
-
Series对象可以随时修改并即刻生效
>>> import pandas as pd
>>> a=pd.Series([1,2,3,4],['a','b','c','d'])
>>> a['a']=10
>>> a.name='Series1'
>>> a
a 10
b 2
c 3
d 4
Name: Series1, dtype: int64
>>> a.name='Series2'
>>> a['b','c']=[15,20]
>>> a
a 10
b 15
c 20
d 4
Name: Series2, dtype: int64
7、总结
-
Series是一维带“标签”数组
-
index_0 ---->data_a
-
Series基本操作类似ndarray和字典,根据索引对齐
网友评论