1-Pandas Series学习笔记
2019-02-24 23:41
155 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_33736653/article/details/87909852
也可以使用
Pandas简介
__Pandas__ 是 Python 中的数据操纵和分析软件包。 它有两个新的数据结构,即 Pandas Series 和 Pandas DataFrame。借助这两个数据结构,我们能够轻松直观地处理带标签数据和关系数据
此课程中,将学习:
- 如何导入 Pandas
- 如何使用各种方法创建 Pandas Series 和 DataFrame
- 如何访问及更改 Series 和 DataFrame 中的元素
- 如何对 Series 执行算术运算
- 如何向 DataFrame 中加载数据
- 如何处理非数 (NaN) 值
Pandas Series 和 DataFrame 专门用于快速进行数据分析和操纵,并且使用灵活简单,具有以下功能:
- 允许为行和列设定标签
- 可以针对时间序列数据计算滚动统计学指标
- 轻松地处理 NaN 值
- 能够将不同格式的数据加载到 DataFrame 中
- 可以将不同的数据集合并到一起
- 与 NumPy 和 Matplotlib 集成
如何创建Pandas Series
Pandas Series 是像数组一样的一维对象。它可以存储多种数据类型。 对象中包含了 data = [ ] 和 index = [ ] 两个属性; Series的显示方式:第一列为索引,第二列为数据。
与 ndarray 的区别:
- Pandas Series 可以为每个元素分配索引标签。即 data 和 index ;
- Pandas Series 可以存储不同类型的数据类型,ndarray 只能储存一种数据类型。
ndarray: - ndarray 只能允许一种数据类型的元素,如果含有不同类型,会自动转型;
( 熟悉 ndarray 的转型规则 )
# 导入 Pandas 包 import pandas as pd # 使用 Series() 函数进行创建 groceries = pd.Series(data = [30,6,'Yes','No'],index = ['egg','break','milk','apple']) groceries
与 NumPy ndarray 一样,Pandas Series 也具有很多函数获取 Series 中的信息。
X.shape:获取 X 的形状;
X.ndim:获取 X 的尺寸;
X.size:获取 X 的元素个数
print('shape:',groceries.shape) print('dimension:',groceries.ndim) print('total elenment:',groceries.size)
shape: (4,) dimension: 1 total elenment: 4
可以使用
X.values和
X.index查询 Series 的数据和索引标签;
也可以使用
in来判断某索引是否存在该 Series 中。
# X.values print('data of Series:',groceries.values) # X.index print('index of Series:',groceries.index) # in 判断 print('bananas' in groceries) print('apple' in groceries)
data of Series: [30 6 'Yes' 'No'] index of Series: Index(['egg', 'break', 'milk', 'apple'], dtype='object') False True
访问和删除元素
Pandas Series 有 两种 访问元素的方式:
-
使用元素中 index 索引值访问。格式:X[One or More index label]
-
使用数字索引,like NumPy ndarray。格式:X[0] or X[-1]
注意: 当含有多个 索引 时,需要多一个 [] ;数字索引时,切片功能同样适用。 -
为了清晰指明引用 标签索引 还是 数字索引 ,Pandas Series 提供了两种属性
.loc
和.iloc
。
.loc
:表示位置,指明使用的是标签索引;
.iloc
:表示整型位置,指明使用的是数字索引。
print(groceries[['egg','milk']]) print(groceries[[1,3]]) # 使用 .loc 和 .iloc 区分 print('loc:',groceries.loc[['egg','milk']]) print('iloc:',groceries.iloc[[1,3]])
egg 30 milk Yes dtype: object break 6 apple No dtype: object loc: egg 30 milk Yes dtype: object iloc: break 6 apple No dtype: object
Pandas 序列是可变的,可以修改序列中的元素。
- 修改元素: X[ index ] = value
- 删除元素: X.drop(index,inplace = False)
— inplace 表示是否在当前 Pandas序列中删除元素;为False时,表示不改变原序列,为True时,表示改变原序列
# 修改元素数据 groceries['egg'] = 3 print(groceries)
egg 3 break 6 milk Yes apple No dtype: object
# 删除元素 groceries.drop('egg') # 不删除原序列元素 print(groceries) groceries.drop('egg',inplace = True) # 删除原序列元素 print(groceries)
egg 3 break 6 milk Yes apple No dtype: objectbreak 6 milk Yes apple No dtype: object
Pandas序列元素的算术运算
- 对单个数字的计算。使得序列中的所有元素都进行该计算。(没有更改原Pandas序列)
- 也可以导入 NumPy 包,对序列进行数学函数计算
# creat a new Pandas Series fruits = pd.Series(data = [5,8,6,7],index = ['bananas','lemon','apple','orange']) # excute operation print(fruits) print() print(fruits + 2) print() print(fruits - 2) print() print(fruits * 2) print() print(fruits / 2) # same result use others like ( - * / )
bananas 5 lemon 8 apple 6 orange 7 dtype: int64 bananas 7 lemon 10 apple 8 orange 9 dtype: int64 bananas 3 lemon 6 apple 4 orange 5 dtype: int64 bananas 10 lemon 16 apple 12 orange 14 dtype: int64 bananas 2.5 lemon 4.0 apple 3.0 orange 3.5 dtype: float64
import numpy as np print(fruits) # print(fruits.mean()) print() print('mean:',np.mean(fruits)) print() print('exp:\n',np.exp(fruits)) print() print('sqrt:\n',np.sqrt(fruits))
bananas 5 lemon 8 apple 6 orange 7 dtype: int64 mean: 6.5 exp: bananas 148.413159 lemon 2980.957987 apple 403.428793 orange 1096.633158 dtype: float64 sqrt: bananas 2.236068 lemon 2.828427 apple 2.449490 orange 2.645751 dtype: float64
Pandas序列还可以对某一特定元素进行算术计算。
- X[One or More index]
+(- * /)
数字 —— 索引值包括标签索引或者数字索引
print(fruits) # label index # fruits[['apple','bananas']] + 2 # fruits.loc[['apple','bananas']] + 2 # interger index fruits[[1,3]] * 2
bananas 5 lemon 8 apple 6 orange 7 dtype: int64 lemon 16 orange 14 dtype: int64
注意:对具有混合数据类型的 Pandas Series 应用算术运算,前提是该算术运算适合 Series 中的所有数据类型,否则会出错
groceries = pd.Series(data = [30,10,'Yes','No'],index = ['egg','apple','bananas','cup']) groceries * 2
egg 60 apple 20 bananas YesYes cup NoNo dtype: object
# but next operation would occur error groceries / 2
相关文章推荐
- 学习笔记:pandas Series形式的数据修改列表名称(columns)
- 我的python学习笔记(4) pandas 之 Series, DataFrame
- pandas学习笔记-Series
- pandas 学习笔记
- 第3章 Pandas数据处理(3.6)_Python数据科学手册学习笔记
- Pandas学习笔记(1)基本数据类型及属性
- Python库--pandas库学习笔记总结
- python 之 pandas 学习笔记--read_excel()
- 机器学习python中pandas库的学习笔记
- pandas学习笔记之用于实现时间序列算法中不平稳序列差分的diff()函数
- Pandas Series笔记
- python 学习笔记【pandas 】
- Pandas学习笔记
- Pandas学习笔记之重复数据统计
- pandas学习笔记
- Pandas 学习笔记
- 『Python学习』pandas进阶学习笔记
- pandas学习笔记(1)--pandas简介
- pandas versus Excel 学习笔记4 (函数填充)
- pandas学习笔记