=== Series 简介 ===
由一组数据和索引组成
数据是各种numpy数据类型
索引值可重复
=== Series 数据创建 ===
# 使用 ndarray 创建
ran_num = np.random.randint(0,10,10)
print(ran_num)
# pd.Series(data,dtype,index) 数据,数据类型,定义索引
pd.Series(ran_num)
print(pd.Series(ran_num,dtype="float32",index=list('abcdefghij')))
[1 6 9 1 8 2 9 4 8 9]
a 1.0 b 6.0 c 9.0 d 1.0 e 8.0 f 2.0 g 9.0 h 4.0 i 8.0 j 9.0 dtype: float32
# 使用列表创建
scores = [90,100,33]
pd.Series(scores,index=["语文","数学","英语"])
语文 90 数学 100 英语 33 dtype: int64
#使用字典创建,字典key会用作索引
scores={"语文":90,"数学":88,"英语":100}
pd.Series(scores)
数学 88 英语 100 语文 90 dtype: int64
注意:字典是无序的,所以输出的内容顺序不一
=== Series 属性 ===
取值
print('-'*10+'Ser_scores'+'-'*10)
Ser_scores = pd.Series(scores)
print(Ser_scores)
print('-'*12+'取索引'+'-'*12)
# 取索引
print(Ser_scores.index)
print(Ser_scores.axes)
print('-'*12+'取值'+'-'*12)
#取值
print(Ser_scores.values)
print('-'*12+'取类型'+'-'*12)
#取类型
print(Ser_scores.dtype)
print('-'*12+'查看维度'+'-'*12)
# 查看维度 ndim
print(Ser_scores.ndim)
print('-'*10+'查看元素个数'+'-'*10)
# 查看元素个数 size
print(Ser_scores.size)
print('-'*10+'查看前2条数据'+'-'*10)
# 前两行 head
print(Ser_scores.head(2))
print('-'*10+'查看后2条数据'+'-'*10)
# 后两行 tail
print(Ser_scores.tail(2))
# 排序
print('-'*12+'升序排序'+'-'*12)
#升序
print(Ser_scores.sort_values())
print('-'*12+'降序排序'+'-'*12)
#降序
print(Ser_scores.sort_values(ascending=False))
----------Ser_scores----------
----------Ser_scores----------
数学 88
英语 100
语文 90
dtype: int64
------------取索引------------
Index(['数学', '英语', '语文'], dtype='object')
[Index(['数学', '英语', '语文'], dtype='object')]
------------取值------------
[ 88 100 90]
------------取类型------------
int64
------------查看维度------------
1
----------查看元素个数----------
3
----------查看前2条数据----------
数学 88
英语 100
dtype: int64
----------查看后2条数据----------
英语 100
语文 90
dtype: int64
------------升序排序------------
数学 88
语文 90
英语 100
dtype: int64
------------降序排序------------
英语 100
语文 90
数学 88
dtype: int64
设置名字
print('-'*12+'给Series设置名字'+'-'*12)
#给Series设置名字
Ser_scores.name="Tom的成绩"
print(Ser_scores)
print('-'*12+'给index设置名字'+'-'*12)
#给index设置名字
Ser_scores.index.name="课程"
print(Ser_scores)
------------给Series设置名字------------
课程
数学 88
英语 100
语文 90
Name: Tom的成绩, dtype: int64
------------给index设置名字------------
课程
数学 88
英语 100
语文 90
Name: Tom的成绩, dtype: int64
判断
# 查看Series是否为空
if(Ser_scores.empty == False):
print('不为空')