数据分析之pandas库--series对象

1.Series属性及方法

  Series是Pandas中最基本的对象,Series类似一种一维数组。

    1.生成对象。创建索引并赋值。

s1=pd.Series()

    2.查看索引和值。

复制代码
s1=Series([1,2,3,4],index=['a','b','c','d'])
s1
运行结果:
a    1
b    2
c    3
d    4
dtype: int64
复制代码

    3.Series有字典的功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
'b' in s1
运行结果:
True
 
list(s1.iteritems())
运行结果:
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
 
dict={"red":1,"black":2,"green":3,"pink":4}
s2=pd.Series(dict)
s2
运行结果:
red      1
black    2
green    3
pink     4
dtype: int64

    4.Series对象的内容和索引都有个name属性。

复制代码
s1.name="word"
s1.index.name="number"
s1
运行结果:
number
a    1
b    2
c    3
d    4
Name: word, dtype: int64
复制代码

    5.用pandas的isnull和nonull可检测缺失数据。

s1.isnull()
运行结果:
number
a    False
b    False
c    False
d    False
Name: word, dtype: bool

2.Series对象存取

   1.Series对象的下标运算同时支持位置和标签两种方式。

print("位置下标:  ",s1[0])
print("标签下标:  ",s1['a'])
运行结果:
位置下标:   1
标签下标:   1

  2.Series对象支持位置切片和标签切片,但需要注意的是后者包括结束标签。

s1[1:3]
运行结果:
number
b    2
c    3
Name: word, dtype: int64

  3.和ndarray数组一样,可以用位置列表、位置数组来存取元素,同样地,标签列表、标签数组也能存取。

s1[[1,3,2]]
运行结果:
number
b    2
d    4
c    3
Name: word, dtype: int64

  4.还可通过索引进行排序(字典中缺失的则用NaN作为内容)。

s1.index=["c","b","a","d"]
s1
运行结果:
c    1
b    2
a    3
d    4
Name: word, dtype: int64

 

posted @   丁如超dd  阅读(694)  评论(0编辑  收藏  举报
编辑推荐:
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
阅读排行:
· Cursor预测程序员行业倒计时:CTO应做好50%裁员计划
· 想让你多爱自己一些的开源计时器
· 大模型 Token 究竟是啥:图解大模型Token
· 用99元买的服务器搭一套CI/CD系统
· 如何在 .NET 中 使用 ANTLR4
点击右上角即可分享
微信分享提示