Series
Series创建
import pandas as pd
import numpy as np
# 2.1 通过list创建
s1 = pd.Series([1,2,3,4,5])
s1
0 1
1 2
2 3
3 4
4 5
dtype: int64
type(s1)
pandas.core.series.Series
# 2.2 通过数组创建
arr1 = np.arange(1,6)
print(arr1)
[1 2 3 4 5]
s2 = pd.Series(arr1)
s2
0 1
1 2
2 3
3 4
4 5
dtype: int32
#索引长度和数据长度必须相同。
s2 = pd.Series(arr1,index=['a','b','c','d','e'])
s2
a 1
b 2
c 3
d 4
e 5
dtype: int32
s1.values
array([1, 2, 3, 4, 5], dtype=int64)
s1.index
RangeIndex(start=0, stop=5, step=1)
# 2.3 通过字典创建
dict = {'name':'李宁','age':18,'class':'三班'}
s3 = pd.Series(dict,index = ['name','age','class','sex']) # 注意一下这里的索引,用字典创建的 Series对象
s3
name 李宁
age 18
class 三班
sex NaN
dtype: object
三.Series的基本用法
# 3.1 isnull 和 notnull 检查缺失值
s3.isnull() #判断是否为空 空就是True
name False
age False
class False
sex True
dtype: bool
s3.notnull() #判断是否不为空 非空True
name True
age True
class True
sex False
dtype: bool
# 3.2 通过索引获取数据
print(s3.index)
print(s3.values)
Index(['name', 'age', 'class', 'sex'], dtype='object')
['李宁' 18 '三班' nan]
# 下标
s3[0]
'李宁'
# 标签名
s3['age']
18
# 选取多个
s3[['name','age']] # s3[[1,3]]
name 李宁
age 18
dtype: object
# 切片
s3[1:3]
age 18
class 三班
dtype: object
s3['name':'class'] #标签切片 包含末端数据
name 李宁
age 18
class 三班
dtype: object
#布尔索引
s2[s2>3]
d 4
e 5
dtype: int32
# 3.3 索引与数据的对应关系不被运算结果影响
print(s2+2)
print(s2>2)
year
a 3
b 4
c 5
d 6
e 7
Name: temp, dtype: int32
year
a False
b False
c True
d True
e True
Name: temp, dtype: bool
# 3.4 name属性
s2.name = 'temp' #对象名
s2.index.name = 'year' #对象索引名
s2
year
a 1
b 2
c 3
d 4
e 5
Name: temp, dtype: int32
s2.head(3) #默认前5行
year
a 1
b 2
c 3
Name: temp, dtype: int32
s2.tail(2) #尾部默认后5行
year
d 4
e 5
Name: temp, dtype: int32