python数据容器专题
Python容器专题
【导读】 容器通常属于序列(如列表或元组)或映射(如字典),但也存在其他形式的容器。依据Python官方的说法,Python语言中标准内建容器包括 dict , list , set , 和 tuple 。本文中的数据容器不单指Python的容器类型,即包括Python序列(又分可变与不可变序列),Python数组,Python数组集合,也包含了来自第三方库扩展的一些可以用于容纳数据的结构如:
【Python序列】:列表、元组、range序列 、字符串序列、可变字节数组序列、不可变字节数组序列…
【Python容器】:集合、字典…
【python标准模块提供的数据容器】:队列、数组,计数器…
【第三方库提供的数据容器】:numpy数组、pandas序列(Series)、pandas数据框(DataFrame)…
近期正在重新排版,可能有部分小节暂时无法阅读。敬请谅解
博主:李俊才
CSDN主页:https://blog.csdn.net/qq_28550263?spm=1001.2101.3001.5343
邮箱 :291148484@163.com
- 内容目录 -
正在重新整理目录…
第Ⅰ篇、Python内置容器
1. Python三大序列类型
在python中,有三种基本序列类型:tuple
(元组)、list
(列表)、range
(不可变的数字序列)。序列类似于C语言等其它编程语言中的数组,其值在一块连续的内存空间中按照一定地顺序组织排列,一般可以通过其位置进行访问。
python中的一些内建模块也提供了序列类型,如Python内建数组array.array
,deque
队列。其中deque是一个双端队列,它在Python编程中尤为常见,它解决了list(列表)从左边插入元素时间复杂度高的不足。
在python的一些第三方模块对Python的内建对象进行了功能补充和性能上的提升,这里面尤为著名的是,Numpy中的array。本文对numpy.array的用法也进行了相应的介绍。
从Python序列的列别上看,可以分为可变序列
和不可变序列
:
可变序列如list
、tuple
不可变序列如str
、bytes
。
1.1 列表(list)
这里仅保留了最常用的功能,点击阅读原详尽内容
创建列表
# 列表的创建
[] # 创建空列表,其结果为[]
[1,2,3,4,5,'6',{7}] # 创建拥有不同数据类型成员的列表,其结果为[1, 2, 3, 4, 5, '6', {7}]
list((0,1)) # 通过强制类型转换创建列表,其结果为[0, 1]
[1,2] * 2 # 简写重复的元素建立一维列表,其结果为 [1, 2, 1, 2][i for i in range(1,16,2)] # 使用嵌套的表达式建立列表,用到了列表推导式的写法,其结果为[1, 3, 5, 7, 9, 11, 13, 15]
list(map(lambda x: x, range(1,16,2))) # 效果上与e相同,其结果为[1, 3, 5, 7, 9, 11, 13, 15] [[1,2,3,4,5],[5,4,3,2,1]] # 创建二维列表,其结果为[[1,2,3,4,5],[5,4,3,2,1]]
[[1,2] * 2] * 3 # 简写重复的元素建立二维列表,# 其结果为 [[1, 2, 1, 2], [1, 2, 1, 2], [1, 2, 1, 2]]
插入元素
# 尾插入法
a = ["你","好"]
a.append("啊") # 在原列表的最后一个位置插入元素
print(a) # 结果为 ["你", "好", "啊"]# 指定位置插入
a = ["你","好"]
a.insert(1,"真") # 在原索引号为 1 的位置处插入元素"真"
print(a) # 结果为 ["你", "真", "好"]
索引列表
# 列表元素的索引
[1, 2, 3, 4][-1] # 其结果为 4
['零','一','二','三','四','五','六'][1:5] # 通过列表的切片创建子列表,其结果为 ['一', '二', '三', '四'] # 元素反查列表索引
a = [i for i in 'ABCDEFGHIJKABCDEFGHIJK']
a.index('G') # 使用列表的index()方法查询'G'的索引
合并列表
# 列表的合并
a = [1, 2, 3, 4]
b = [5, 6, 7, 8]
a+b # 其结果为 [1, 2, 3, 4, 5, 6, 7, 8]
a.extend(b) # 其结果为 [1, 2, 3, 4, 5, 6, 7, 8]# 列表合并字典键
a = [0, 1, 2, 3, 4]
b = {'5':'五','6':'六','7':'气'}
a.extend(b) # 其结果为 [0, 1, 2, 3, 4, '5', '6', '7']
元素存在性
a = ["老虎","狮子","豹子","袍子","猴子","猩猩","狗熊"]
"北极熊" in a # 判断某元素是否在列表a中,结果为 False
“"猴子" in a # 结果为 True
"袍子" not in a # 结果为 False
拷贝列表
# 获取列表的一个浅拷贝
a = [0, 1, 2, 3, 4, 3, 2, 1]
a.copy()
统计列表
# 列表元素的统计
a = [i for i in 'ABCDEFGHIJKABCDEFGHIJK']
a.count('A') # 其结果为 2,表明该元素在列表中出现过2次
len(a) # 统计列表 a 中所有元素的个数,结果为22
max(a) # 最大值,对于字符比较的是字符的最后的字母。返回结果为'K'
min(a) # 最小值,对于字符比较的是字符的最前的字母。返回结果为'A'
反转列表
# 反转
a = [1, 2, 3, 4, 5, 6, 7]
a.reverse() # 反转列表元素,[7, 6, 5, 4, 3, 2, 1]
列表排序
# 排序
a = [ 'zl赵六', 'lh李华', 'lm李明', 'xh小红','zs张三', 'lh李华', 'ww王五', 'zl赵六', 'xh小红', 'lh李华']
print(a.sort()) # 对列表中的元素进行排序# 结果为 [‘lh李华’, ‘lh李华’, ‘lh李华’, ‘lm李明’, ‘ww王五’, ‘xh小红’, ‘xh小红’, ‘zl赵六’, ‘zl赵六’, ‘zs张三’]b = [9,7,8,5,2,4,5,4,5]
b.sort()
print(b) # 结果为 [2, 4, 4, 5, 5, 5, 7, 8, 9]
注意:列表的
sort()
方法本身并不返回值,它会改变原列表元素的顺序。如果仅想返回一个排序后的列表,可以使用sorted()
语句,该语句不会改变原列而返回一个新的排序后的列表,如:a = [9,7,8,5,2,4,5,4,5] print("a=",a) sorted(a)
1.2 元组(tuple)
这里仅保留了最常用的功能,点击阅读原详尽内容
创建元组
# 元组的创建
() # 直接创建一个空元组
(1, 2) # 通过列举成员直接创建一个一维元组
((1,2,3),(4,5,6),(7,8,9)) # 通过列举成员直接创建一个二维元组
1, 2, '3','4', 5 # 创建序列甚至可以不写小括号,且其成员支持任意类型
索引元组
# 索引元组
tp = tuple(range(10))
tp[2] # tp的第3个元素,其结果为 2
a_tuple[2:6] # tp的第3个元素到第6个元素构成的子元组,其结果为 (2, 3, 4, 5)
其它类型转换为元组
# 将其它序列转换为元组
tuple([1,2,3]) # 将列表转换为元组,结果为(1,2,3)
tuple("写代码很快乐") # 将字符串转换为元组,结果为 ("写","代","码","很","快","乐")
tuple((i for i in range(5))) # 有元组推导式生成元组,结果为(0,1,2,3,4)
对元组进行统计
a = (6,4,7,8,9,2,4)
len(a) # 获取元组a中的元素个数,结果为 7
max(a) # 求元组a中的最大值,结果为 9
min(a) # 求元组a中的最小值,结果为 2
元素存在性
a = ("老虎","狮子","豹子","袍子","猴子","猩猩","狗熊")
"北极熊" in a # 判断某元素是否在元组a中,结果为 False
“"猴子" in a # 结果为 True
"袍子" not in a # 结果为 False
1.3 range序列
这里不得不提,如果你使用的是Python2而不是Python3,那么range()得到的是一个列表。然而在Python3中,range ()函数得到的是一个独立的数据结构,它虽然也是序列类型但并非列表,仅是一个可迭代对象。
创建range序列
range(5) # 创建range序列,结果为 range(0,5)
索引range序列
range(0,5)[2:3] # 索引range序列,结果为 range(2, 3)
range(0,10)[2:8:2] # 索引range序列时也同样可以指定在最后步长,结果为 range(2, 8, 2),包含元素2,4,6
range(10)[2] # 结果为 2
元素反求索引
range(10).index(5) # 查元素5在该range序列中的索引号,结果为5
统计range序列
len(range(5)) # 求range序列中的元素个数,结果为 5
max(range(3,9)) # 求range序列中最大的元素,结果为8
min(range(3,9)) # 求range序列中最小的元素,结果为3
元素存在性
0 in range(5) # 判断元素是否在range序列中,结果为 True
5 in range(5) # 结果为 False
转换为其它序列
tuple(range(2,5)) # range序列转换为元组,结果为 (2, 3, 4)
list(range(2,5)) # range序列转换为列表,结果为 [2, 3, 4]
list(range(0, -10, -1)) # 结果为 [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
list(range(1, 0)) # 结果为 []
list(range(0)) # 结果为 []
2. Python内建其它序列类型
2.1 文本序列类型(srt)
这里仅保留了最常用的功能,点击阅读原详尽内容
在Python语言中,文本序列类型(srt)即字符串类型,它是由 Unicode 码位构成的不可变序列。虽然额外提供了以下列出的一些附加方法,但需要了解的是字符串类型作为序列类型的一种,同样实现了所有一般序列的操作。作为容器专题,我们关注的重点放在其作为Unicode码位构成的序列容器上,而并不打算讨论文本处理。如果想完整了解字符串,尤其是其在文本处理上的相关方法,以及字符串匹配与正则表达式等内容,可以参考上面文字连接,跳转到字符串专题。
创建字符串
"abcd" # 既可以使用双引号创建字符串
'WIJDH' # 也可以使用单引号创建字符串
a = "Hello World!" # 将字符串常量赋值给遍历`a`,那么变量`a`引用的是字符串类型对象# 但注意Python是动态类型语言,当再次复制其它类型的时候a就会称为对其它对象的引用
索引字符串
字符串索引[a, b]是第a到第b-1个元素,其中索引号从0开始计算。
a = "ie6DCEG5QSD4Rwjgc"
a[2:6] # 结果为'6DCE'
字符串添加元素
连接字符串与其它容器
元素反查索引
查找字符串可以使用find()方法,该方法用法如下:
str.find(sub[, start[, end]])
返回子字符串 sub 在 s[start:end] 切片内被找到的最小索引。 可选参数 start 与 end 会被解读为切片表示法。 如果 sub 未被找到则返回 -1。
元素的替换
元素存在性
字符串统计
转换为其它序列类型
2.2 二进制序列类型
2.2.1 bytes
对象 - 单字节构成的不可变序列
创建不可变字节数组
可以使用bytes()
函数创建可修改的字节数组对象,该函数的语法格式为:
bytes([source[,encoding[, errors]]])
- source:int, str, 或者可迭代对象
- encoding: 将source表示的内容转换为字节数据时采用的字符编码,默认使用’utf-8’编码
- errors: 报错级别,如
'stict'
(严格级别,字符编码由报错即抛出异常)、'ignore'
(忽略级别,忽略掉字符编码错误)、'replace'
(替换级别,字符编码有错误则替换为"?")、
bytes(b"kajshd") # 直接由二进制数据创建不可变字节数组,只有是ASCII码中包含的字符才可以。结果为 b'kajshd'
bytes("我爱我的祖国", "utf-8") # 指定编码为"utf-8",将字符串转为不可变字节数组,b'\xe6\x88\x91\xe7\x88\xb1\xe4\xb8\xad\xe5\x9b\xbd'
bytes(6) # 结果为 b'\x00\x00\x00\x00\x00\x00'
bytes([1,2,3,4,5,6]) # 结果为 b'\x01\x02\x03\x04\x05\x06'
bytes((1,2,3,4,5,6)) # 结果为 b'\x01\x02\x03\x04\x05\x06'
2.2.2 bytearray
对象 - 可修改的字节数组
创建字节数组
可以使用bytearray()
函数创建可修改的字节数组对象,该函数的语法格式为:
bytearray([source[,encoding[, errors]]])
- source:int, str, 或者可迭代对象
- encoding: 将source表示的内容转换为字节数据时采用的字符编码,默认使用’utf-8’编码
- errors: 报错级别,如
'stict'
(严格级别,字符编码由报错即抛出异常)、'ignore'
(忽略级别,忽略掉字符编码错误)、'replace'
(替换级别,字符编码有错误则替换为"?")、
bytearray() # 创建一个空字节数组,返回结果为 bytearray(b'')
bytearray(range(5)) # 由range序列创建字节数组,结果为 bytearray(b'\x00\x01\x02\x03\x04')
bytearray([1,2,3]) # 由列表创建字节数组,结果为 bytearray(b'\x01\x02\x03')
bytearray((1,2,3)) # 由元组创建字节数组,结果为 bytearray(b'\x01\x02\x03')
bytearray(b'1a63fb') # 由二进制数组创建字节数组,结果为 bytearray(b'1a63fb')
bytearray(5) # 由整数创建二进制数组,其结果为 bytearray(b'\x00\x00\x00\x00\x00')bytearray("我爱python", "utf-8") # 指定字符串编码为"utf-8",由字符串创建字节数组# 其结果为 bytearray(b'\xe6\x88\x91\xe7\x88\xb1python')
bytearray("我爱python", "gbk") # 指定字符串编码为"gbk",由字符串创建字节数组# 其结果为 bytearray(b'\xce\xd2\xb0\xaepython')
【注意】:
- 如果参数source为字符串(str),则必须指定encoding,并且
bytearray()
函数是使用str.endode()
方法将字符串转换为字节数据的。 - 如果参数source为整数(int),则返回这个整数所指定长度的空
统计字节数组
a = bytearray([6,4,5,7,8,9,4,5,6,4,6])
len(a) # 字节数组 a 的长度,结果为11
max(a) # 字节数组 a 元素最大值,结果为 9
min(a) # 字节数组 a 元素最大值,结果为 4
a.count(6) # 字节数组 a 中 6 对应字节数据出现次数,结果为 3
修改字节数组
a = bytearray("快乐学习", "utf-8")
print(a) # 结果为bytearray(b'\xe5\xbf\xab\xe4\xb9\x90\xe5\xad\xa6\xe4\xb9\xa0'),一个汉字有3个字节
a.pop() # 出栈,返回 160 ,原字节数组变为 bytearray(b'\xe5\xbf\xab\xe4\xb9\x90\xe5\xad\xa6\xe4\xb9')
a.push(1) # 尾插,a 变为 bytearray(b'\xe5\xbf\xab\xe4\xb9\x90\xe5\xad\xa6\xe4\xb9\x01')
a.insert(8,6) # 在索引为 8 处插入,a 变为 bytearray(b'\xe5\xbf\xab\xe4\xb9\x90\xe5\xad\x06\xa6\xe4\xb9\x01')
a.index(6) # 获取第一个6对应字节数据的索引,结果为 8
索引字节数组
a = bytearray([6,4,5,7,8,9,4,5,6,4,6])
a[5] # 索引字节数组第 6 个元素,结果为整数 9
a[4:8] # 索引第 5 到第 7 个字节元素,结果为字节数组 bytearray(b'\x08\t\x04\x05')
元素存在性
a = bytearray([6,4,5,7,8,9,4,5,6,4,6])
6 in a # 判断 6 的字节数据是否在字节数组 a 中,结果为 True
7 not in a # 判断 7 的字节数据是否在字节数组 a 中,结果为 False
例:创建字节数据并写入文本
a = bytearray("你好啊,世界!","utf-8")
path = r"D:\codes\bytearray_test.txt"
with open(path,"wb") as file:file.write(a)
例:读取文本文件中的内容,以字符串和字节的形式分别输出
path = r"D:\codes\bytearray_test.txt"
with open(path,'rb') as file:stream = file.read()print("输出字节流:", stream)print("转为字符串输出", str(straem, encoding="utf-8"))
3. 通用序列操作
每个不同的序列类型都有自己的一些特性,但既然都称之为序列必然是有很多的交集。本文相当于一个小结,归纳了在各种序列中都存在的一些通用的操作。
运算 | 结果 | 注释 |
---|---|---|
x in s
|
如果 s 中的某项等于 x 则结果为 True ,否则为 False
|
(1) |
x not in s
|
如果 s 中的某项等于 x 则结果为 False ,否则为 True
|
(1) |
s + t
|
s 与 t 相拼接 | (6)(7) |
s[i]
|
s 的第 i 项,起始为 0 | (3) |
s[i:j]
|
s 从 i 到 j 的切片 | (3)(4) |
s[i:j:k]
|
s 从 i 到 j 步长为 k 的切片 | (3)(5) |
len (s)
|
s 的长度 | |
min (s)
|
s 的最小项 | |
max (s)
|
s 的最大项 | |
s.index (x[, i[, j]])
|
x 在 s 中首次出现项的索引号(索引号在 i 或其后且在 j 之前) | (8) |
s.count (x)
|
x 在 s 中出现的总次数 |
4. Python容器类型
4.1 容器类型概述
4.2 集合(set)
这里仅保留了最常用的功能,点击阅读原详尽内容
4.3 字典(dict)
这里仅保留了最常用的功能,点击阅读原详尽内容
第Ⅱ篇、Python标准库的扩展的容器
1. array(数组)-- 高效的数值数组
这里仅保留了最常用的功能,点击阅读原详尽内容
from array import array # 导入python数组对象
2. deque(队列)–双向队列对象
这里仅保留了最常用的功能,点击阅读原详尽内容
快速双端队列对象(deque)是一个类似列表(list)的容器,,实现了在两端快速添加(append)和弹出(pop)。
导入队列对象
from collections import deque
插入元素
deque([1,2,3,4,5,6,7]).append('1') # 尾插法:结果为 deque([1, 2, 3, 4, 5, 6, 7, '1']),时间复杂度为O(1)
deque('abcde').appendleft('A') # 首查法:结果为 deque(['A', 'a', 'b', 'c', 'd', 'e']),时间复杂度也为O(1)
deque('abcde').insert(2,'666') # 在指定索引位置插入元素,结果为 deque(['a', 'b', '666', 'c', 'd', 'e'])
拓展队列
a = deque('abcde')
a.extend('ABC') # 在原队列右侧拓展,结果为 deque(['a', 'b', 'c', 'd', 'e', 'A', 'B', 'C'])
a = deque('abcde')
a.extend(['*',6,'&',9,'^']) # 结果为 deque(['a', 'b', 'c', 'd', 'e', '*', 6, '&', 9, '^'])
第Ⅲ篇、重要第三方库提供的容器
1. numpy数组
这里仅保留了最常用的功能,点击阅读原详尽内容
import numpy as np
a = np.array([1,2,3,4,5,6]) # 通过将列表[1,2,3,4,5,6]类型转换创建数组a
b = np.array((1,2,3,4,5,6)) # 通过将元组(1,2,3,4,5,6)类型转换创建数组b
c = np.arange(0,1,7,dtype=int) # 等间隔生成数组c,指定元素数据类型为整形。该方法类似于原生Python中用rang(i)生成range序列,但返回的是一个np.array对象
d = np.linspace(-2,2,5) # 在(-2,2)上按照线性间隔创建数组
e = np.empty((2,3),int) # 通np.empty()方法创建 2X3 空数组
f = np.random.random((2, 3)) # 生成2X3的随机浮点数数组
g = np.random.randint(1,10,(3,5)) # 随机生成区间[1:9]上的 3行5列数组
h = np.mgrid[-2:2:5j] # j代表虚数单位,此处与np.linspace(-2,2,5)等价
i = np.mgrid[0:2:4j,10:20:5j] # 使用虚数单位j生成区间[0,2]X[10,20]上的4X5的二维数组
print('a=',a,'\nb=',b,'\nc=',c,'\nd=',d,'\ne=',e,'\nf=',f,'\ng=',g,'\nh=',h,'\ni=',i)
Out[]:
a= [1 2 3 4 5 6]
b= [1 2 3 4 5 6]
c= [0]
d= [-2. -1. 0. 1. 2.]
e= [[1 2 3][4 5 6]]
f= [[0.21883881 0.80893396 0.86216125][0.47656256 0.05026984 0.92402366]]
g= [[3 5 6 2 8][9 5 4 3 3][5 9 6 1 8]]
h= [-2. -1. 0. 1. 2.]
i= [
[[ 0. 0. 0. 0. 0. ][ 0.66666667 0.66666667 0.66666667 0.66666667 0.66666667][ 1.33333333 1.33333333 1.33333333 1.33333333 1.33333333][ 2. 2. 2. 2. 2. ]][[10. 12.5 15. 17.5 20. ][10. 12.5 15. 17.5 20. ][10. 12.5 15. 17.5 20. ][10. 12.5 15. 17.5 20. ]]]
2. pandas中的容器
这里主要提供查询相关方法和属性及其注释,如有不理解或者想阅读代码实例
请点击本处文字链接阅读原详尽内容
2.1 Series
2.1.1 导入Series对象
from pandas import Series # 这样导入可以直接使用 Series
import pandas as pd # 这样导入需要写成 pandas.pd
2.1.2 创建 Series对象实例
由数组或序列创建Series
from pandas import Series
Series([1,2,3,4,5,6])
Out[]:
0 1
1 2
2 3
3 4
4 5
5 6
dtype: int64
从具有指定索引的字典创建Series
import pandas as pd
a = {'a': 1, 'b': 2, 'c': 3}
pd.Series(data=a, index=['a', 'b', 'c'])
Out[]:
a 1
b 2
c 3
dtype: int64
2.1.3 获取Series基本信息的属性
属性 | 说明 |
---|---|
Series.index
|
Series的索引(轴标签) |
Series.array
|
支持该Series 或索引的数据的扩展数组 |
Series.values
|
根据dtype将Series返回为ndarray或类似ndarray的形式 |
Series.dtype
|
返回基础数据的dtype对象. |
Series.shape
|
返回基础数据形状的元组 |
Series.nbytes
|
返回基础数据中的字节数 |
Series.ndim
|
根据定义1,基础数据的维数 |
Series.size
|
返回基础数据中的元素数量 |
Series.memory_usage
|
返回Series的内存使用情况 |
Series.hasnans
|
如果有nan 则返回,支持各种性能加速
|
Series.dtypes
|
返回基础数据的dtype对象 |
Series.name
|
返回Series的名字 |
Series.flags
|
获取与pandas相关对象的属性 |
属性/方法 | 说明 |
---|---|
Series.T
|
返回其转置 |
Series.empty
|
指示DataFrame是否为空 |
2.1.4 对Series进行转换的相关方法
方法 | 说明 |
---|---|
Series.astype (dtype[, copy, errors])
|
将pandas对象转换为指定的dtype dtype |
Series.convert_dtypes ([infer_objects, …])
|
使用dtypes support将列转换为最佳的dtype pd.NA |
Series.infer_objects ()
|
尝试为对象列推断更好的dtype |
Series.copy ([deep])
|
复制该对象的索引和数据 |
Series.bool()
|
返回单个元素Series或DataFrame的布尔值 |
Series.to_numpy ([dtype, copy, na_value])
|
表示此Series或Index中的值的NumPy ndarray。 |
Series.to_period ([freq, copy])
|
将Series从DatetimeIndex转换为PeriodIndex |
Series.to_timestamp ([freq, how, copy])
|
在时段开始时转换为时间戳的DatetimeIndex |
Series.to_list()
|
返回值列表 |
Series.__array__ ([dtype])
|
以NumPy数组形式返回值 |
2.1.5 Series 的合并、比较和连接
方法/属性 | 说明 |
---|---|
Series.append (to_append[, ignore_index, …])
|
串联两个或多个Series |
Series.compare (other[, align_axis, …])
|
与另一个系列比较并显示差异 |
Series.update (other)
|
使用传递的Series中的值在适当位置修改Serie,other表示的是另一个Series |
2.1.6 Series 索引与迭代
属性/方法 | 说明 |
---|---|
Series.get (key[,default])
|
从对象获取给定键的元素(例如:DataFrame的列) |
Series.at
|
访问行/列标签对的单个值 |
Series.iat
|
通过整数位置访问行/列对的单个值 |
Series.loc
|
通过标签或布尔数组访问一组行和列 |
Series.iloc
|
纯粹基于整数位置的索引,用于按位置进行选择 |
Series.__iter__ ()
|
返回值的迭代器 |
Series.items ()
|
懒惰地遍历(索引,值)元组 |
Series.iteritems ()
|
懒惰地遍历(索引,值)元组 |
Series.keys ()
|
返回索引的别名 |
Series.pop (item)
|
返回项目和系列中的下落 |
Series.item ()
|
以Python标量返回基础数据的第一个元素 |
Series.xs (key[,axis,level,drop_level])
|
从Series / DataFrame返回横截面(cross-section) |
2.1.7 Series 统计与计算
属性/方法 | 说明 |
---|---|
Series.abs ()
|
返回具有每个元素的绝对数值的Series / DataFrame。 |
Series.all ([axis, bool_only, skipna, level])
|
返回是否所有元素都为True(可能在某个轴上) |
Series.any ([axis, bool_only, skipna, level])
|
返回是否有任何元素为True,可能在某个轴上 |
Series.autocorr ([lag])
|
计算滞后-N自相关 |
Series.between (left, right[, inclusive])
|
返回等于left <= series <= right的布尔Series |
Series.clip ([lower, upper, axis, inplace])
|
修剪输入阈值处的值 |
Series.corr (other[, method, min_periods])
|
计算与其他Series的相关性,排除缺失值 |
Series.count ([level])
|
返回Series中非NA /空观测值的数量 |
Series.cov (other[, min_periods, ddof]))
|
计算与序列的协方差,不包括缺失值 |
Series.cummax ([axis, skipna])
|
返回DataFrame或Series轴上的累积最大值 |
Series.cummin ([axis, skipna])
|
返回DataFrame或Series轴上的累积最小值 |
Series.cumprod ([axis, skipna])
|
通过DataFrame或Series轴返回累积乘积 |
Series.cumsum ([axis, skipna])
|
返回DataFrame或Series轴上的累积总和 |
Series.describe ([percentiles, include, …])
|
生成描述性统计信息 |
Series.diff ([periods])
|
元素的第一个离散差 |
Series.factorize ([sort, na_sentinel])
|
将对象编码为枚举类型或分类变量 |
Series.kurt ([axis, skipna, level, numeric_only])
|
在请求的轴上返回无偏峰度 |
Series.mad ([axis, skipna, level])
|
返回所请求轴上的值的平均绝对差 |
Series.max ([axis, skipna, level, numeric_only])
|
返回请求轴上的最大值 |
Series.mean ([axis, skipna, level, numeric_only])
|
返回所请求轴上的值的平均值 |
Series.median ([axis, skipna, level, …])
|
返回请求轴上的值的中位数 |
Series.min ([axis, skipna, level, numeric_only])
|
返回请求轴上的最小值 |
Series.mode ([dropna])
|
返回Series的模式 |
Series.nlargest ([n, keep])
|
返回最大的n个元素 |
Series.nsmallest ([n, keep])
|
返回最小的n个元素 |
Series.pct_change ([periods, fill_method, …])
|
当前元素与先前元素之间的百分比变化 |
Series.prod ([axis, skipna, level, …])
|
返回所请求轴上的值的乘积 |
Series.quantile ([q, interpolation])
|
给定分位数的返回值 |
Series.rank ([axis, method, numeric_only, …])
|
计算沿轴的数值数据等级(1到n) |
Series.sem ([axis, skipna, level, ddof, …])
|
返回所请求轴上的平均值的无偏标准差 |
Series.skew ([axis, skipna, level, numeric_only])
|
在请求的轴上返回无偏斜 |
Series.std ([axis, skipna, level, ddof, …])
|
返回要求轴上的样品标准差 |
Series.sum ([axis, skipna, level, …])
|
返回请求轴上的值之和 |
Series.var ([axis, skipna, level, ddof, …])
|
返回请求轴上的无偏方差 |
Series.kurtosis ([axis, skipna, level, …])
|
在请求的轴上返回无偏峰度 |
Series.unique ()
|
返回Series对象的唯一值 |
Series.nunique ([dropna])
|
返回对象中唯一元素的数量 |
Series.is_unique
|
如果对象中的值是唯一的,则返回布尔值 |
Series.is_monotonic
|
如果对象中的值是monotonic_increasing,则返回boolean |
Series.is_monotonic_increasing
|
is_monotonic的别名 |
Series.is_monotonic_decreasing
|
如果对象中的值是monotonic_decreasing,则返回boolean |
Series.value_counts ([normalize, sort, …])
|
返回一个包含唯一值计数的Series |
2.1.8 Series索引相关
属性/方法 | 说明 |
---|---|
Series.align (other[, join, axis, level, …])
|
使用指定的join方法将两个对象在其轴上对齐 |
Series.drop ([labels, axis, index, columns, …])
|
返回系列,其中删除了指定的索引标签 |
Series.droplevel (level[, axis])
|
返回已删除请求的索引/列级别的DataFrame |
Series.drop_duplicates ([keep, inplace])
|
返回具有重复值的Series |
Series.duplicated ([keep])
|
指示重复的系列值 |
Series.equals (other)
|
测试两个对象是否包含相同的元素 |
Series.first (offset)
|
根据日期偏移量选择时间序列数据的初始时段 |
Series.head ([n])
|
返回前n行 |
Series.idxmax ([axis, skipna])
|
返回最大值的行标签。 |
Series.idxmin ([axis, skipna])
|
返回最小值的行标签。 |
Series.isin (values)
|
Series中的元素是否包含在value中 |
Series.last (offset)
|
根据日期偏移量选择时间序列数据的最后时段 |
Series.reindex ([index])
|
通过可选的填充逻辑使Series与新索引一致 |
Series.reindex_like (other[, method, copy, …])
|
返回具有匹配索引的对象作为其他对象 |
Series.rename ([index, axis, copy, inplace, …])
|
更改系列索引标签或名称 |
Series.rename_axis ([mapper, index, columns, …])
|
设置索引或列的轴名称 |
Series.reset_index ([level, drop, name, inplace])
|
生成一个新的带有重置索引的DataFrame或Series |
Series.sample ([n, frac, replace, weights, …])
|
从对象轴返回随机的项目样本 |
Series.set_axis (labels[, axis, inplace])
|
将所需的索引分配给给定的轴 |
Series.take (indices[, axis, is_copy])
|
沿轴返回给定位置索引中的元素 |
Series.tail ([n])
|
返回最后n行 |
Series.truncate ([before, after, axis, copy])
|
在某个索引值之前和之后截断Series或DataFrame |
Series.where (cond[, other, inplace, axis, …])
|
替换条件为False的值 |
Series.mask (cond[, other, inplace, axis, …])
|
替换条件为True的值 |
Series.add_prefix (prefix)
|
带字符串前缀的前缀标签 |
Series.add_suffix (suffix)
|
带字符串后缀的后缀标签 |
Series.filter ([items, like, regex, axis])
|
根据指定的索引标签对DataFrame的行或列进行子集设置 |
2.1.9 Series 排序相关
方法/属性 | 说明 |
---|---|
Series.argsort ([axis, kind, order])
|
返回将对Series值进行排序的整数索引 |
Series.argmin ([axis, skipna])
|
返回Series中最小值的int位置 |
Series.argmax ([axis, skipna])
|
返回该系列中最大值的int位置 |
Series.reorder_levels (order)
|
使用输入顺序重新排列索引级别 |
Series.sort_values ([axis, ascending, …])
|
按值排序 |
Series.sort_index ([axis, level, ascending, …])
|
按索引标签对系列进行排序 |
Series.swaplevel ([i, j, copy])
|
将i和j交换为MultiIndex |
Series.unstack ([level, fill_value])
|
Unstack(也称为数据透视),使用MultiIndex进行Series生成DataFrame |
Series.explode ([ignore_index])
|
将类似列表的每个元素转换为一行 |
Series.searchsorted (value[, side, sorter])
|
查找应在其中插入元素以保持顺序的索引 |
Series.ravel ([order])
|
将展平的基础数据作为ndarray返回 |
Series.repeat (repeats[, axis])
|
重复系列的元素 |
Series.squeeze ([axis])
|
将一维轴对象压缩为标量 |
Series.view ([dtype])
|
创建该系列的新视图 |
2.1.10 Series 缺失值处理相关
方法/属性 | 说明 |
---|---|
Series.backfill ([axis, inplace, limit, downcast])
|
DataFrame.fillna()与的同义词method=‘bfill’ |
Series.bfill ([axis, inplace, limit, downcast]))
|
DataFrame.fillna()与的同义词method=‘bfill’ |
Series.dropna ([axis, inplace, how])
|
返回删除了缺失值的新系列 |
Series.ffill ([axis, inplace, limit, downcast]))
|
DataFrame.fillna()与的同义词method=‘ffill’ |
Series.fillna ([value, method, axis, …]))
|
使用指定的方法填充NA / NaN值 |
Series.interpolate ([method, axis, limit, …]))
|
使用插值方法填充NaN值 |
Series.isna ()
|
检测缺失值 |
Series.isnull ()
|
检测缺失值 |
Series.notna ()
|
检测现有(非缺失)值 |
2.1.11 时间序列相关
方法/属性 | 说明 |
---|---|
Series.asfreq (freq[, method, how, …])
|
将TimeSeries转换为指定的频率 |
Series.asof (where[, subset])
|
返回where之前没有任何NaN的最后一行 |
Series.shift ([periods, freq, axis, fill_value])
|
将索引按期望的周期数移动,并带有可选的时间频率 |
Series.first_valid_index ()](#pandas.Series.first_valid_index)
|
返回第一个非NA /空值的索引 |
Series.last_valid_index ()](#pandas.Series.last_valid_index)
|
返回上一个非NA /空值的索引 |
Series.resample (rule[, axis, closed, label, …])
|
重新采样时间序列数据 |
Series.tz_convert (tz[, axis, level, copy])
|
将可感知tz的轴转换为目标时区 |
Series.tz_localize (tz[, axis, level, copy, …])
|
将Series或DataFrame的tz天真索引本地化为目标时区 |
2.1.12 日期与时间相关
方法/属性 | 说明 |
---|---|
Series.dt.date
|
返回python datetime.date对象的numpy数组(即,没有时区信息的Timestamps的日期部分 |
Series.dt.time
|
返回datetime.time的numpy数组 |
Series.dt.timetz
|
返回datetime.time的numpy数组,其中还包含时区信息 |
Series.dt.year
|
日期时间的年份 |
Series.dt.month
|
月份为1月= 1,12月= 12 |
Series.dt.day
|
日期时间的日期 |
Series.dt.hour
|
日期时间 |
Series.dt.minute
|
日期时间的分钟数 |
Series.dt.second
|
日期时间的秒数 |
Series.dt.microsecond
|
日期时间的微秒 |
Series.dt.nanosecond
|
日期时间的纳秒 |
Series.dt.weekofyear
|
(不推荐)一年中的第几周 |
Series.dt.dayofweek
|
星期一= 0,星期日= 6的星期几 |
Series.dt.day_of_week
|
星期一= 0,星期日= 6的星期几 |
Series.dt.weekday
|
星期一= 0,星期日= 6的星期几 |
Series.dt.dayofyear
|
一年中的第几天 |
Series.dt.day_of_year
|
一年中的第几天 |
Series.dt.quarter
|
日期的四分之一 |
Series.dt.is_month_start
|
指示日期是否为每月的第一天 |
Series.dt.is_month_end
|
指示日期是否为每月的最后一天 |
Series.dt.is_quarter_start
|
日期是否为一个季度的第一天的指标 |
Series.dt.is_quarter_end
|
日期是否为一个季度的最后一天的指标 |
Series.dt.is_year_start
|
指明日期是否为一年的第一天 |
Series.dt.is_year_end
|
指示日期是否为一年的最后一天 |
Series.dt.is_leap_year
|
布尔值指示符,指示日期是否属于a年 |
Series.dt.daysinmonth
|
月中的天数 |
Series.dt.days_in_month
|
月中的天数 |
Series.dt.tz
|
返回时区(如果有) |
Series.dt.freq
|
返回此PeriodArray的频率对象 |
Series.dt.to_period (*args, **kwargs)
|
以特定频率转换为PeriodArray / Index。 |
Series.dt.to_pydatetime ()
|
以原生Python日期时间对象数组的形式返回数据 |
Series.dt.tz_localize (*args, **kwargs)
|
将tz天真日期时间数组/索引本地化为可感知tz的日期时间数组/索引 |
Series.dt.tz_convert (*args, **kwargs)
|
将可感知tz的Datetime数组/索引从一个时区转换为另一个时区 |
Series.dt.normalize (*args, **kwargs)
|
将时间转换为午夜 |
Series.dt.strftime (*args, **kwargs)
|
使用指定的date_format转换为Index |
Series.dt.round (*args, **kwargs)
|
对数据执行舍入运算到指定的频率 |
Series.dt.floor (*args, **kwargs)
|
对指定频率的数据执行发言权操作 |
Series.dt.ceil (*args, **kwargs)
|
对数据执行ceil操作至指定的频率 |
Series.dt.month_name (*args, **kwargs)
|
返回具有指定语言环境的DateTimeIndex的月份名称 |
Series.dt.day_name (*args, **kwargs)
|
返回具有指定语言环境的DateTimeIndex的日期名称 |
Series.dt.days
|
每个元素的天数 |
Series.dt.seconds
|
每个元素的秒数(> = 0且少于1天) |
Series.dt.microseconds
|
每个元素的微秒数(> = 0且小于1秒)。 |
Series.dt.nanoseconds
|
每个元素的纳秒数(> = 0且小于1微秒) |
Series.dt.components
|
返回Timedeltas组件的Dataframe |
Series.dt.to_pytimedelta ()
|
返回本地datetime.timedelta对象的数组 |
Series.dt.total_seconds (*args, **kwargs)
|
返回每个元素的总持续时间,以秒为单位 |
2.2 DataFrame
这里仅保留了最常用的功能,点击阅读原详尽内容
2.2.1 底层数据和基本信息相关
属性/方法 | 说明 |
---|---|
DataFrame.index
|
DataFrame的索引(行标签) |
DataFrame.columns
|
DataFrame的列标签 |
DataFrame.dtypes
|
返回DataFrame中的dtype |
DataFrame.info ([verbose, buf, max_cols, …])
|
打印DataFrame的简要摘要 |
DataFrame.select_dtypes ([include, exclude])
|
根据列dtypes返回DataFrame列的子集 |
DataFrame.values
|
返回DataFrame的Numpy表示形式。 |
DataFrame.axes
|
返回一个表示DataFrame轴的列表。 |
DataFrame.ndim
|
返回一个表示轴数/数组维数的整数。 |
DataFrame.size
|
返回一个表示此对象中元素数量的int |
DataFrame.shape
|
返回一个表示DataFrame维数的元组 |
DataFrame.memory_usage ([index, deep])
|
返回每列的内存使用情况(以字节为单位) |
DataFrame.empty
|
指示DataFrame是否为空 |
DataFrame.set_flags (*[, copy, …])
|
返回带有更新标志的新对象 |
2.2.2 转换相关
属性/方法 | 说明 |
---|---|
DataFrame.astype (dtype[, copy, errors])
|
将pandas对象转换为指定的dtype dtype。 |
DataFrame.convert_dtypes ([infer_objects, …])
|
使用dtypes support将列转换为最佳的dtype pd.NA |
DataFrame.infer_objects ()
|
尝试为对象列推断更好的dtype |
DataFrame.copy ([deep])
|
复制该对象的索引和数据 |
DataFrame.bool ()
|
返回单个元素Series或DataFrame的布尔值 |
2.2.3 索引与迭代相关
属性/方法 | 说明 |
---|---|
DataFrame.head ([n])
|
返回前n行 |
DataFrame.at
|
访问行/列标签对的单个值 |
DataFrame.iat
|
通过整数位置访问行/列对的单个值 |
DataFrame.loc
|
通过标签或布尔数组访问一组行和列 |
DataFrame.iloc
|
纯粹基于整数位置的索引,用于按位置进行选择 |
DataFrame.insert (loc, column, value[, …])
|
将列插入到DataFrame中的指定位置 |
DataFrame.__iter__ ()
|
遍历信息轴。 |
DataFrame.items ()
|
遍历(列名,系列)对 |
DataFrame.iteritems ()
|
遍历(列名,系列)对。 |
DataFrame.keys ()
|
获取“信息轴”(有关更多信息,请参见索引) |
DataFrame.iterrows ()
|
将DataFrame行作为(索引,系列)对进行迭代 |
DataFrame.itertuples ([index, name])
|
以namedtuple的形式遍历DataFrame行 |
DataFrame.lookup (row_labels, col_labels)
|
(不推荐)用于DataFrame的基于标签的“花式索引”功能 |
DataFrame.pop (item))
|
返回项目并从框架中放下。 |
DataFrame.tail ([n])
|
返回最后n行 |
DataFrame.xs (key[, axis, level, drop_level])
|
从Series / DataFrame返回横截面 |
DataFrame.get (key[, default])
|
从对象获取给定键的项目(例如:DataFrame列) |
DataFrame.isin (values)
|
DataFrame中的每个元素是否包含在值中 |
DataFrame.where (cond[, other, inplace, …])
|
替换条件为False的值 |
DataFrame.mask (cond[, other, inplace, axis, …])
|
替换条件为True的值 |
DataFrame.query (expr[, inplace])
|
使用布尔表达式查询DataFrame的列 |
2.2.4 统计、计算相关
属性/方法 | 说明 |
---|---|
DataFrame.abs ()
|
返回具有每个元素的绝对数值的Series / DataFrame。 |
DataFrame.all ([axis, bool_only, skipna, level])
|
返回是否所有元素都为True(可能在某个轴上) |
DataFrame.any ([axis, bool_only, skipna, level])
|
返回是否有任何元素为True(可能在某个轴上) |
DataFrame.clip ([lower, upper, axis, inplace])
|
修剪输入阈值处的值。 |
DataFrame.corr ([method, min_periods])
|
计算列的成对相关,不包括NA / null值 |
DataFrame.corrwith (other[, axis, drop, method])
|
计算成对相关 |
DataFrame.count ([axis, level, numeric_only]))
|
为每一列或每一行计算非NA单元。 |
DataFrame.cov ([min_periods, ddof]))
|
计算列的成对协方差,不包括NA /空值 |
DataFrame.cummax ([axis, skipna]))
|
返回DataFrame或Series轴上的累积最大值 |
DataFrame.cummin ([axis, skipna])
|
返回DataFrame或Series轴上的累积最小值 |
DataFrame.cumprod ([axis, skipna])
|
通过DataFrame或Series轴返回累积乘积 |
DataFrame.cumsum ([axis, skipna])
|
返回DataFrame或Series轴上的累积总和 |
DataFrame.describe ([percentiles, include, …])
|
生成描述性统计信息 |
DataFrame.diff ([periods, axis])
|
元素的第一个离散差 |
DataFrame.eval (expr[, inplace])
|
评估一个字符串,该字符串描述对DataFrame列的操作 |
DataFrame.kurt ([axis, skipna, level, …])
|
在请求的轴上返回无偏峰度 |
DataFrame.kurtosis ([axis, skipna, level, …])
|
在请求的轴上返回无偏峰度 |
DataFrame.mad ([axis, skipna, level])
|
返回所请求轴上的值的平均绝对偏差 |
DataFrame.max ([axis, skipna, level, …])
|
返回请求轴上的最大值 |
DataFrame.mean ([axis, skipna, level, …])
|
返回所请求轴上的值的平均值 |
DataFrame.median ([axis, skipna, level, …])
|
返回请求轴上的值的中位数 |
DataFrame.min ([axis, skipna, level, …])
|
返回请求轴上的最小值 |
DataFrame.mode ([axis, numeric_only, dropna])
|
获取沿选定轴的每个元素的模式 |
DataFrame.pct_change ([periods, fill_method, …])
|
当前元素与先前元素之间的百分比变化 |
DataFrame.prod ([axis, skipna, level, …])
|
返回所请求轴上的值的乘积 |
DataFrame.product ([axis, skipna, level, …])
|
返回所请求轴上的值的乘积 |
DataFrame.quantile ([q, axis, numeric_only, …])
|
在请求的轴上以给定的分位数返回值 |
DataFrame.rank ([axis, method, numeric_only, …])
|
计算沿轴的数值数据等级(1到n) |
DataFrame.round ([decimals])
|
将DataFrame舍入到小数位数可变 |
DataFrame.sem ([axis, skipna, level, ddof, …])
|
返回所请求轴上的平均值的无偏标准误差 |
DataFrame.skew ([axis, skipna, level, …])
|
在请求的轴上返回无偏斜 |
DataFrame.sum ([axis, skipna, level, …])
|
返回请求轴上的值之和 |
DataFrame.std ([axis, skipna, level, ddof, …])
|
返回要求轴上的样品标准偏差 |
DataFrame.var ([axis, skipna, level, ddof, …])
|
返回请求轴上的无偏方差 |
DataFrame.nunique ([axis, dropna])
|
在请求的轴上计数不同的观察值 |
DataFrame.value_counts ([subset, normalize, …])
|
返回一个包含DataFrame中唯一行数的Series |
2.2.5 二进制计算相关
方法/属性 | 说明 |
---|---|
DataFrame.add (other[, axis, level, fill_value])
|
获取数据帧和其他元素的加法(二进制运算符add) |
DataFrame.sub (other[, axis, level, fill_value])
|
获取数据帧和其他元素的减法(二进制运算符sub) |
DataFrame.mul (other[, axis, level, fill_value])
|
获取数据帧和其他逐元素的乘法(二进制运算符mul) |
DataFrame.div (other[, axis, level, fill_value])
|
获取数据帧和其他逐元素的浮点除法(二进制运算符truediv) |
DataFrame.truediv (other[, axis, level, …])
|
获取数据帧和其他逐元素的浮点除法(二进制运算符truediv) |
DataFrame.floordiv (other[, axis, level, …])
|
获取数据帧和其他逐元素的整数分割(二进制运算符floordiv) |
DataFrame.mod (other[, axis, level, fill_value])
|
获取数据帧的模数和其他逐元素的模数(二进制运算符mod) |
DataFrame.pow (other[, axis, level, fill_value])
|
获取数据帧和其他逐元素的指数幂(二进制运算符pow) |
DataFrame.dot (other)](#pandas.DataFrame.dot)
|
计算DataFrame与其他框架之间的矩阵乘法 |
DataFrame.radd (other[, axis, level, fill_value])
|
获取数据帧和其他元素的加法(二进制运算符radd) |
DataFrame.rsub (other[, axis, level, fill_value])
|
获取数据帧和其他逐元素减法(二进制运算符rsub) |
DataFrame.rmul (other[, axis, level, fill_value])
|
获取数据帧和其他逐元素的乘法(二进制运算符rmul) |
DataFrame.rdiv (other[, axis, level, fill_value])
|
获取数据帧和其他逐元素的浮点除法(二进制运算符rtruediv) |
DataFrame.rtruediv (other[, axis, level, …])
|
获取数据帧和其他逐元素的浮点除法(二进制运算符rtruediv) |
DataFrame.rfloordiv (other[, axis, level, …])
|
获取数据帧和其他逐元素的整数分割(二进制运算符rfloordiv) |
DataFrame.rmod (other[, axis, level, fill_value])
|
获取数据帧的Modulo以及其他元素(二进制运算符rmod) |
DataFrame.rpow (other[, axis, level, fill_value])
|
获取数据帧和其他逐元素的指数幂(二进制运算符rpow) |
DataFrame.lt (other[, axis, level])
|
获取小于dataframe和其他元素形式的值(二进制运算符lt) |
**DataFrame.gt (other[, axis, level]) **
|
获得大于dataframe和元素级的其他值(二进制运算符gt) |
DataFrame.le (other[, axis, level])
|
获取小于或等于dataframe和其他逐元素(二进制运算符le) |
DataFrame.ge (other[, axis, level])
|
获取大于或等于dataframe和其他逐元素(二进制运算符ge)的值 |
DataFrame.ne (other[, axis, level])
|
获取不等于dataframe和其他按元素计的值(二进制运算符ne) |
DataFrame.eq (other[, axis, level])
|
等于等于dataframe和其他逐元素(二进制运算符eq) |
DataFrame.combine (other, func[, fill_value, …])
|
与另一个DataFrame进行按列合并 |
DataFrame.combine_first (other)
|
在other中相同位置的值中更新null元素 |
2.2.6 索引、选取、标签操作相关
属性/方法 | 说明 |
---|---|
DataFrame.add_prefix (prefix)
|
带字符串前缀的前缀标签 |
DataFrame.add_suffix (suffix)
|
带字符串后缀的后缀标签 |
DataFrame.align (other[, join, axis, level, …])
|
使用指定的join方法将两个对象在其轴上对齐 |
DataFrame.at_time (time[, asof, axis])
|
选择一天中特定时间(例如,上午9:30)的值 |
DataFrame.between_time (start_time, end_time)
|
选择一天中特定时间(例如9:00-9:30 AM)之间的值 |
DataFrame.drop ([labels, axis, index, …])
|
从行或列中删除指定的标签 |
DataFrame.drop_duplicates ([subset, keep, …])
|
返回删除了重复行的DataFrame |
DataFrame.duplicated ([subset, keep])
|
返回表示重复行的布尔系列 |
DataFrame.equals (other)](#pandas.DataFrame.equals)
|
测试两个对象是否包含相同的元素 |
DataFrame.filter ([items, like, regex, axis])
|
根据指定的索引标签对DataFrame的行或列进行子集设置 |
DataFrame.first (offset)
|
根据日期偏移量选择时间序列数据的初始时段 |
DataFrame.head ([n])
|
返回前n行 |
DataFrame.idxmax ([axis, skipna])
|
返回在请求轴上第一次出现最大值的索引 |
DataFrame.idxmin ([axis, skipna])
|
返回在请求轴上第一次出现最小值的索引 |
DataFrame.last (offset)
|
根据日期偏移量选择时间序列数据的最后时段 |
DataFrame.reindex ([labels, index, columns, …])
|
使用可选的填充逻辑使Series / DataFrame适应新索引 |
DataFrame.reindex_like (other[, method, …])
|
返回具有匹配索引的对象作为其他对象 |
DataFrame.rename ([mapper, index, columns, …])
|
更改轴标签 |
DataFrame.rename_axis ([mapper, index, …])
|
设置索引或列的轴名称 |
DataFrame.reset_index ([level, drop, …])
|
重置索引或索引的级别 |
DataFrame.sample ([n, frac, replace, …])
|
从对象轴返回随机的项目样本 |
DataFrame.set_axis (labels[, axis, inplace])
|
将所需的索引分配给给定的轴 |
DataFrame.set_index (keys[, drop, append, …])
|
使用现有列设置DataFrame索引 |
DataFrame.tail ([n])
|
返回最后n行 |
DataFrame.take (indices[, axis, is_copy])
|
沿轴返回给定位置索引中的元素 |
DataFrame.truncate ([before, after, axis, copy])
|
在某个索引值之前和之后截断Series或DataFrame |
2.2.7 分组操作相关
属性/方法 | 说明 |
---|---|
DataFrame.apply (func[, axis, raw, …])
|
沿DataFrame的轴应用功能 |
DataFrame.applymap (func[, na_action])
|
将一个函数应用于Dataframe元素 |
DataFrame.pipe (func, *args, **kwargs)
|
应用func(self,* args,** kwargs) |
DataFrame.agg ([func, axis])
|
使用指定轴上的一项或多项操作进行汇总 |
DataFrame.aggregate ([func, axis])
|
使用指定轴上的一项或多项操作进行汇总 |
DataFrame.transform (func[, axis])
|
调用funcself产生具有转换值的DataFrame |
DataFrame.groupby ([by, axis, level, …])
|
使用映射器或按一系列列对DataFrame进行分组 |
DataFrame.rolling (window[, min_periods, …])
|
提供滚动窗口计算 |
DataFrame.expanding ([min_periods, center, axis])
|
提供扩展的转换 |
DataFrame.ewm ([com, span, halflife, alpha, …])
|
提供指数加权(EW)函数 |
2.2.8 缺失的数据处理相关
属性/方法 | 说明 |
---|---|
DataFrame.backfill ([axis, inplace, limit, …])
|
DataFrame.fillna()与的同义词method=‘bfill’ |
DataFrame.bfill ([axis, inplace, limit, downcast])
|
DataFrame.fillna()与的同义词method=‘bfill’ |
DataFrame.dropna ([axis, how, thresh, …])
|
删除缺失的值 |
DataFrame.ffill ([axis, inplace, limit, downcast])
|
DataFrame.fillna()与的同义词method=‘ffill’ |
DataFrame.fillna ([value, method, axis, …])
|
使用指定的方法填充NA / NaN值 |
DataFrame.interpolate ([method, axis, limit, …])
|
使用插值方法填充NaN值 |
DataFrame.isna ()
|
检测缺失值 |
DataFrame.isnull ()
|
检测缺失值 |
DataFrame.notna ()
|
检测现有(非缺失)值 |
DataFrame.notnull ()
|
检测现有(非缺失)值 |
DataFrame.pad ([axis, inplace, limit, downcast])
|
DataFrame.fillna()与的同义词method=‘ffill’ |
DataFrame.replace ([to_replace, value, …])
|
将to_replace中给出的值替换为value |
2.2.9 形状操作相关
属性/方法 | 说明 |
---|---|
DataFrame.droplevel (level[, axis])
|
返回已删除请求的索引/列级别的DataFrame |
DataFrame.pivot ([index, columns, values])
|
返回按给定的索引/列值组织的重整型DataFrame |
DataFrame.pivot_table ([values, index, …])
|
创建一个电子表格样式的数据透视表作为DataFrame |
DataFrame.reorder_levels (order[, axis])
|
使用输入顺序重新排列索引级别 |
DataFrame.sort_values (by[, axis, ascending, …])
|
任一轴的值排序 |
DataFrame.sort_index ([axis, level, …])
|
按标签(沿轴)对对象排序 |
DataFrame.nlargest (n, columns[, keep])
|
返回按列降序排列的前n行 |
DataFrame.nsmallest (n, columns[, keep])
|
回按列升序排列的前n行 |
DataFrame.swaplevel ([i, j, axis])
|
在特定轴上的MultiIndex中交换级别i和j |
DataFrame.stack ([level, dropna])
|
从列到索引堆叠指定级别 |
DataFrame.unstack ([level, fill_value])
|
枢转一个级别(必要的层次结构)索引标签 |
DataFrame.swapaxes (axis1, axis2[, copy])
|
适当地交换轴和交换值轴 |
DataFrame.melt ([id_vars, value_vars, …])
|
取消将DataFrame从宽格式转为长格式,可以选择保留标识符 |
DataFrame.explode (column[, ignore_index])
|
将类似列表的每个元素转换为一行,复制索引值 |
DataFrame.squeeze ([axis])
|
将一维轴对象压缩为标量 |
DataFrame.to_xarray ()
|
从pandas对象返回一个xarray对象 |
DataFrame.T
|
获取DataFrame的转置 |
DataFrame.transpose (*args[, copy])
|
转置索引和列 |
2.2.10 增加、连接、比较、合并相关
属性/方法 | 说明 |
---|---|
DataFrame.append (other[, ignore_index, …])
|
将其他行附加到调用方的末尾,并返回一个新对象 |
DataFrame.assign (**kwargs)
|
将新列分配给DataFrame |
DataFrame.compare (other[, align_axis, …])
|
与另一个DataFrame进行比较并显示差异 |
DataFrame.join (other[, on, how, lsuffix, …])
|
连接另一个DataFrame的列 |
DataFrame.merge (right[, how, on, left_on, …])
|
用数据库样式的联接合并DataFrame或命名的Series对象 |
2.2.11 与时间序列相关
属性/方法 | 说明 |
---|---|
DataFrame.asfreq (freq[, method, how, …])
|
将TimeSeries转换为指定的频率 |
DataFrame.asof (where[, subset])
|
返回where之前没有任何NaN的最后一行 |
DataFrame.shift ([periods, freq, axis, …])
|
将索引按期望的周期数移动,并带有可选的时间频率 |
DataFrame.first_valid_index ()
|
返回第一个非NA /空值的索引 |
DataFrame.last_valid_index ()
|
返回上一个非NA /空值的索引 |
DataFrame.resample (rule[, axis, closed, …])
|
重新采样时间序列数据 |
DataFrame.to_period ([freq, axis, copy])
|
将DataFrame从DatetimeIndex转换为PeriodIndex |
DataFrame.to_timestamp ([freq, how, axis, copy])
|
在时段开始时将其强制转换为时间戳的DatetimeIndex |
DataFrame.tz_convert (tz[, axis, level, copy])
|
将可感知tz的轴转换为目标时区 |
DataFrame.tz_localize (tz[, axis, level, …])
|
将Series或DataFrame的tz天真索引本地化为目标时区 |
python数据容器专题相关推荐
- python入门(六)——python数据容器
系列文章目录 python入门(一)--你好python python入门(二)--python基础语法 python入门(三)--python判断语句 python入门(四)--python循环语句 ...
- Python数据容器:list + set + tuple + dict + str 简记 黑马课程
# -*- coding:utf8 -*- """ # editor: hjjdreamer # create-time: 2022/11/16-23:08 # Pyth ...
- 【Python基础】Python数据容器(集合)
文章目录 数据容器:set(集合) 集合的定义 集合的常用操作-修改 (1)添加新元素 (2)移除元素 (3)从集合中随机取出元素 (4)清空集合 (5)取出 两个集合的差集 (6)消除 两个集合的差 ...
- 【Python基础】Python数据容器(字典)
文章目录 数据容器:dict(字典.映射) 字典的定义 字典数据的获取 字典的嵌套 字典的注意事项 字典的常用操作 (1)新增元素 (2)更新元素 (3)删除元素 (4)清空字典 (5)获取全部key ...
- 【Python基础】Python数据容器(元组)
文章目录 数据容器:tuple(元组) 元组的定义格式 元组的相关操作 元组的相关操作-注意事项 元组的特点 数据容器:tuple(元组) 元组的定义格式 元组定义:定义元组使用小括号,且使用逗号隔开 ...
- 【Python基础】Python数据容器(字符串)
文章目录 数据容器:str(字符串) 字符串的常用操作 (1)查找特定字符串的下标索引值 (2)字符串的替换 (3)字符串的分割 (4)字符串的规整操作(去前后空格) (5)字符串的规整操作(去前后指 ...
- python数据容器之集合、字典
set集合 基本语法: # 定义集合字面量 (元素,元素,--元素) # 定义集合变量 变量名称 = (元素,元素,--元素) # 定义空集合 变量名称 = set() 列表使用 [] 元组使用 () ...
- 3. Python 数据容器(列表、元组、字符串、集合、字典)
1. 数据容器入门 数据容器:一种可以容纳多份数据的数据类型,容纳的每份数据称为一个元素. 每个元素,可以是任意类型数据,如:字符串.数字.布尔等. 数据容器根据特点的不同,如: 是否支持重复元素 是 ...
- Python数据容器(五)
python学习之旅(五) 学习汇总入口[Python]学习汇总(3万字+思维导图) 写作不易,如果您觉得写的不错,欢迎给博主来一波点赞.收藏~让博主更有动力吧! 一.数据容器 一种可以容纳多份数据的 ...
最新文章
- 详解Pattern类和Matcher类
- Linux系统中的函数
- h5压缩图片上传 php_一键压缩,图片上传大小不得超过200K?
- python能进行切片操作的数据类型可以是_Python新手学习基础之数据类型——字符串的切片截取...
- C++中实现 time_t, tm 相互转换
- SAP CAM - Cloud Access Manager
- 教大家白嫖图床,有的小伙伴跟我说图床不好整,太麻烦
- 存数据返回他的序列号id_雪花般的分布式唯一ID雪花算法
- 走进JavaWeb技术世界4:Servlet 工作原理详解
- [毕设分享]基于华为eNSP的校园网设计和仿真模拟
- 软件测试 白盒测试用例设计方法动态 逻辑覆盖(语句覆盖、判定覆盖、条件覆盖、判定条件覆盖、条件组合覆盖、路径覆盖)基本路径测试法
- 推荐歌曲《 起风了》
- 华三交换机升级的ipe文件_H3C S5830V2[S5820V2]系列以太网交换机 配置指导-Release 2108-6W101...
- C++实现费马小定理素数判定法和米勒拉宾素数判定算法生成大素数
- Win10下的WSL(Linux子系统)开发环境搭建(PHP+Nginx+Mysql+Composer)
- IDEA官方有中文版啦
- JS类中event的简单实现
- java中workFlowEvent_关于WorkFlow的使用以及例子
- 阿米洛键盘取消win_阿米洛(Varmilo)
- 关于小米路由器青春版刷老固件