虫虫

Pandas库的Series类型(北理大嵩天MOOC课程笔记)

2019-03-01  本文已影响64人  手抓饭和馕

1、Series类型

>>> import  pandas as pd
>>> a=pd.Series([1,2,3,4])
>>> a
0    1
1    2
2    3
3    4
dtype: int64
>>> 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

2、Series类型创建

>>> 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
>>> 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类型的基本操作

>>> 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

• 索引方法相同,采用[]
• 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

• 通过自定义索引访问
• 保留字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对齐操作

>>> 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属性

>>> 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类型的修改

>>> 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、总结

上一篇 下一篇

猜你喜欢

热点阅读