03 pandas Series_删改查、运算、缺省

简介: === 新增、删除===# 新增/修改result['化学'] = 100# 删除del result['语文']=== 取值操作 ===# 取值操作 print('-'*10+'Ser_scores'+'-'*10)scores={...

=== 新增、删除===

# 新增/修改
result['化学'] = 100
# 删除
del result['语文']

=== 取值操作 ===

# 取值操作 
print('-'*10+'Ser_scores'+'-'*10)
scores={"语文":90,"数学":88,"英语":100,"物理":98,"化学":95,"生物":92}
Ser_scores = pd.Series(scores)
print(Ser_scores)

# 取单个值
print('---- 取单个值 ----')
print(Ser_scores['化学'])

# 取多个值
print('---- 取多个值 ----')
print(Ser_scores[['化学','语文']])

# 取范围值 切片
# 注意:如果索引值是数值类型,那么切片操作不包含结束值
print('-- 数值切片[2:4] 4不包含 --')
print(Ser_scores[2:4])

# 注意:如果索引值是object类型,包含结束值
print('-- object切片 英语包含 --')
print(Ser_scores['物理':'英语']) 

# 使用下标取值
print('-- 使用下标取值 --')
print(Ser_scores[[0,1,2]]) 
----------Ser_scores----------
化学     95
数学     88
物理     98
生物     92
英语    100
语文     90
dtype: int64
---- 取单个值 ----
95
---- 取多个值 ----
化学    95
语文    90
dtype: int64
-- 数值切片[2:4] 4不包含 --
物理    98
生物    92
dtype: int64
-- object切片 英语包含 --
物理     98
生物     92
英语    100
dtype: int64
-- 使用下标取值 --
化学    95
数学    88
物理    98
dtype: int64
# 取值操作 
print('-'*10+'Ser_scores'+'-'*10)
Ser_scores = pd.Series([1,2,3],index=list('abb'))
print(Ser_scores)

# 同名index,会取出多个值
print('--- 同名index 取出多个值---')
print(Ser_scores['b'])

注意:出现同名index,切片会有问题 ['b':'b']

----------Ser_scores----------
a    1
b    2
b    3
dtype: int64
--- 同名index 取出多个值---
b    2
b    3
dtype: int64

=== Series 运算===

print('-'*10+'Ser_scores'+'-'*10)
scores={"语文":90,"数学":88,"英语":100,"物理":98,"化学":95,"生物":92}
Ser_scores = pd.Series(scores)
print(Ser_scores)

# 数学运算符
print('--- '+'除法'+' ---')
print(Ser_scores/2)

# 使用numpy运算
print('--- '+'开方'+' ---')
print(np.sqrt(Ser_scores))
----------Ser_scores----------
化学     95
数学     88
物理     98
生物     92
英语    100
语文     90
dtype: int64
--- 除法 ---
化学    47.5
数学    44.0
物理    49.0
生物    46.0
英语    50.0
语文    45.0
dtype: float64
--- 开方 ---
化学     9.746794
数学     9.380832
物理     9.899495
生物     9.591663
英语    10.000000
语文     9.486833
dtype: float64

注意:基本上可以把Series看成Numpy中的ndarray数组来操作

# Series 运算
print('-'*10+'Ser_scores'+'-'*10)
scores1={"语文":90,"数学":88,"英语":100,"物理":98,"化学":95,"生物":92}
scores2={"语文":100,"数学":100,"英语":100,"物理":100,"化学":100,"生物":100}
Ser_scores1 = pd.Series(scores1,name='Tom的成绩')
Ser_scores2 = pd.Series(scores2,name='Bai的成绩')
# 计算总分
Ser_scores1 + Ser_scores2
----------Ser_scores----------
化学    195
数学    188
物理    198
生物    192
英语    200
语文    190
dtype: int64

注意:如果其中一个Series缺少index,计算会出现NaN)

=== 缺失值检测 ===

print('--- 判断哪些值是NAN ---')
scores={"语文":90,"数学":88,"英语":np.nan}
result = pd.Series(scores,name='Tom的成绩')
# 找到NaN返回True
print(result.isnull())

# 用bool索引取出NaN值
print('--- 处理NaN的值 ---')
result[result.isnull()] = 0
print(result)
--- 判断哪些值是NAN ---
数学    False
英语     True
语文    False
Name: Tom的成绩, dtype: bool
--- 处理NaN的值 ---
数学    88.0
英语     0.0
语文    90.0
Name: Tom的成绩, dtype: float64
相关文章
|
4月前
|
BI 数据处理 索引
Pandas基本操作:Series和DataFrame(Python)
Pandas基本操作:Series和DataFrame(Python)
314 1
|
4月前
|
索引 Python
Python 教程之 Pandas(11)—— 索引和选择 series 的数据
Python 教程之 Pandas(11)—— 索引和选择 series 的数据
53 0
Python 教程之 Pandas(11)—— 索引和选择 series 的数据
|
4月前
|
索引 Python
Python 教程之 Pandas(10)—— 访问 series 的元素
Python 教程之 Pandas(10)—— 访问 series 的元素
80 0
Python 教程之 Pandas(10)—— 访问 series 的元素
|
4月前
|
存储 SQL 索引
Python 教程之 Pandas(9)—— 创建 Pandas Series
Python 教程之 Pandas(9)—— 创建 Pandas Series
65 0
Python 教程之 Pandas(9)—— 创建 Pandas Series
|
4月前
|
JSON 数据挖掘 数据格式
Pandas中Series、DataFrame讲解及操作详解(超详细 附源码)
Pandas中Series、DataFrame讲解及操作详解(超详细 附源码)
163 0
|
22天前
|
Python
掌握pandas中的时序数据分组运算
掌握pandas中的时序数据分组运算
|
21天前
|
索引 Python
Pandas学习笔记之Series
Pandas学习笔记之Series
|
22天前
|
数据挖掘 Linux Go
全平台都能用的pandas运算加速神器
全平台都能用的pandas运算加速神器
|
9月前
|
前端开发 Python
Python 教程之 Pandas(12)—— series 的二元运算
Python 教程之 Pandas(12)—— series 的二元运算
56 0
|
1月前
|
SQL 数据采集 JSON
Pandas 使用教程 Series、DataFrame
Pandas 使用教程 Series、DataFrame
33 0