02 pandas Series_创建、属性

=== 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('不为空')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容