import numpy as np
import pandas as pd
s1 = pd.Series([1,2,3,4])
s1 #series 包含两部分内容 索引和内容
0 1
1 2
2 3
3 4
dtype: int64
s1.values
array([1, 2, 3, 4], dtype=int64)
s1.index
RangeIndex(start=0, stop=4, step=1)
s2 = pd.Series(np.arange(10))
s2
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
dtype: int32
s2.values
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
s2.index
RangeIndex(start=0, stop=10, step=1)
s3 = pd.Series({"1":1,"2":2,"3":3})
s3
1 1
2 2
3 3
dtype: int64
s3.values
array([1, 2, 3], dtype=int64)
s3.index
Index([‘1’, ‘2’, ‘3’], dtype=‘object’)
s4 = pd.Series([1,2,3,4],index=["A","B","C","D"])
s4
A 1
B 2
C 3
D 4
dtype: int64
s4.values
array([1, 2, 3, 4], dtype=int64)
s4.index
Index([‘A’, ‘B’, ‘C’, ‘D’], dtype=‘object’)
s4["A"] #此处series用法与字典同
1
s4[s4>2] #series可以这么用,字典不行
C 3
D 4
dtype: int64
s4.to_dict() #转换成字典
{‘A’: 1, ‘B’: 2, ‘C’: 3, ‘D’: 4}
s5 = pd.Series(s4.to_dict()) #与字典的互换
s5
A 1
B 2
C 3
D 4
dtype: int64
#series 的 index 是可以改变的
index_1=["A","B","C","D","E"]
s6 = pd.Series(s5, index = index_1)
s6
A 1.0
B 2.0
C 3.0
D 4.0
E NaN
dtype: float64
pd.isnull(s6) #查看s6中哪个index的value是null(NaN)
A False
B False
C False
D False
E True
dtype: bool
pd.notnull(s6) #查看s6中哪个index的value不是null(NaN)
A True
B True
C True
D True
E False
dtype: bool
#可以为series起名,也可以为series的index起名
s6.name = "demo"
s6
A 1.0
B 2.0
C 3.0
D 4.0
E NaN
Name: demo, dtype: float64
s6.index.name = "demo index"
s6.index
Index([‘A’, ‘B’, ‘C’, ‘D’, ‘E’], dtype=‘object’, name=‘demo index’)
s6.values
array([ 1., 2., 3., 4., nan])