没有合适的资源?快使用搜索试试~ 我知道了~
首页Pandas 主要知识点.pdf
资源详情
资源评论
资源推荐

Pandas 主要知识点
Pandas 数据类型例子
# 载入 pandas 库
import pandas as pd
# 创建 Series 数据类型
s = pd.Series([2, 4, 6, 8, 10])
# 创建 DataFrame 数据类型
d = pd.DataFrame([
[2, 4, 6, 8, 10],
[12, 14, 16, 18, 20]
])
s
d
0 2
1 4
2 6
3 8
4 10
dtype: int64
0
1
2
3
4
0
2
4
6
8
1
0
1
1
2
1
4
1
6
1
8
2
0
因为 Series 和 DataFram 方法用的非常多,也可以将其引入本地命名空间方便代码
书写
from pandas import Series, DataFrame
s = Series([2, 4, 6, 8, 10])
d = DataFrame([
[2, 4, 6, 8, 10],
[12, 14, 16, 18, 20]
])
s
d
数据类型-Series

import numpy as np
import pandas as pd
s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
s
• Series 对象:一维的,带标签的数组(类似字典或加索引的一维数组)
• Series 对象是 ndarray 数组的派生类型,和数组相比类型没有限制
• Series 对象本质上由两个 ndarray 数组构成,一个数组构成对象的键(index,索
引,标签),一个数组构成对象的值(values),键 -> 值
• Numpy 中的数组运算和操作都可用于 Series 对象(Series 本质上是 ndarray)
• Python 字典操作部分可用于 Series(Series 内部实现的部分)
Series 对象的操作(创建和查增改删)
C,create,创建
Python list 列表 创建 Series
# 不设置 index 时使用默认索引 0 1 2 3...n-1,位置
class1 = pd.Series([100, 25, 50, 90, 60])
# 自定义索引,标签
class1 = pd.Series([100, 25, 59, 90, 61], index=['ming', 'hua',
'hong', 'huang', 'bai'])
# 可存储不同数据类型
hong = pd.Series([True, 18, 59.5, '小红'], index=['sex', 'age',
'grade', 'name'])
type(hong[0]), type(hong[3])
Python 字典 创建 Series
class1 = pd.Series({'hong': 50, 'huang': 90, 'qing': 60})
# 修改字典索引
class1_values = {'hong': 50, 'huang': 90, 'qing': 60}
class1_index = ['hong', 'huang', 'lan']
class1 = pd.Series(class1_values, index=class1_index)
其他方式 创建 Series
# ndarray 创建 Series,索引和数据都可以通过 ndarray 类型生成
f = pd.Series(np.arange(5))
g_values = np.arange(5)
g_index = np.arange(9, 4, -1)
g = pd.Series(g_values, index = g_index)
# 某些序列函数创建 Series,例如可迭代对象
range(10)

list(range(10)) # 迭代器
h = pd.Series(range(10))
# 标量值创建 Series
i = pd.Series(10)
j = pd.Series(10, index=['a', 'b', 'c'])
R,read,查询
index 和 value 查询
class1
# 值数据,输出类型为 array,还是 ndarray 数组
class1.values
# 索引,输出 index 类型(Pandas 独有的索引类型),本质上就是 ndarray
class1.index
class1.index[2]
class1.index.values
索引查询
class1
# 查询单值
## 位置索引,默认,自动生成,和自定义索引并存
class1[1]
## 标签索引,自定义索引
class1['hua']
class1.hua # 方法调用写法
# 查询多值
class1[[1, 3, 4]]
class1[['hua', 'huang', 'bai']] # 注意双括号
# b[[1, 'hua', 'huang']] #错误,两套索引并存,但不能混用
切片
class1
# 位置切片,默认索引,左闭右开
class1[:3]
# 标签切片,自定义索引
# 注意:两边都闭区间(因为使用标签索引时通常不知道标签顺序,很难确定结束
前一个标签是什么)

class1[:'huang']
class1['huang':]
# 步长
class1[::2]
class1[::-1] #步长-1,逆序
类似 ndarray 的数组运算
如根据布尔数组进行过滤、标量算术运算、应用数学函数等,会保留索引和值之间
的关系链接
class1
# 过滤
class1 >= 60
class1[class1 >= 60]
# 标量算数运算
class1 + 1
# 应用函数
np.median(class1) # 中位数
class1.median() # 方法调用写法
类似 Python 字典的操作
• 保留字 in 操作
• 使用.get()方法
class1
'c' in class1 # 判断此键在不在 class1 的索引中
# 0 in class1 # 错误,in 不会判断自动索引
class1.get('zi', 60) #从 class1 中提取索引 zi 的值,如果存在就取出,不存在
用 60 代替
U,update,修改
read 查询选中赋值即可修改
class1
# values 值修改
class1['ming'] = 0
class1[['hua','hong']] = 55
class1[['hua','hong']] = [35, 55]
class1['hua','hong'] = [1, 2] # 一层也可以
class1

# 索引修改
class2 = class1.copy() # 复制副本而非引用视图,类似深拷贝
class2.index = ['xiaoming', 'xiaohua', 'xiaohong','xiaohuang',
'xiaobai']
剩余47页未读,继续阅读


















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0