Pandas---2.内部数据结构(Index/MultiIndex/Series/DataFrame内部结构)
undefined
(实线为普通属性,虚线为property
属性或者getset_descriptor
)
1.Index
的结构
.name
为普通属性,返回Index
的名字.values/._values
为property
属性,返回Index
的内部数据的视图._data
为普通属性,返回Index
的内部数据.shape
为property
属性,返回内部数据的形状._engine
为标签映射管理器,它负责管理label
和下标之间的映射ObjectEngine
对象使用一个哈希表对象PyObjectHashTable
对象(由ObjectEngine
对象的.mmaping
属性给出,该属性是一个getset_descriptor
)将标签映射到其对应的整数下标的。
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import numpy as np import pandas as pd s = pd.Index([ 'a' , 'b' , 'c' , 'd' ],name = 'index1' ) s # Index(['a', 'b', 'c', 'd'], dtype='object', name='index1') s.name,s.values,s._values # ('index1', array([ 'a' , 'b' , 'c' , 'd' ], dtype = object ), array([ 'a' , 'b' , 'c' , 'd' ], dtype = object )) s._data,s.shape, # (array(['a', 'b', 'c', 'd'], dtype=object), (4,)) s._engine # <pandas._libs.index.ObjectEngine at 0x1e1671803b8> |
2.MultiIndex
的结构
.name
为普通属性,返回MultiIndex
的名字。同Index
.values/._values
为property
属性,返回MultiIndex
的内部数据的视图。同Index
._data
为None
,这里是与Index
不同。
.shape
为property
属性,返回内部属性的形状 。同Index
._engine
为标签映射管理器,它负责管理label
和下标之间的映射。同Index
.labels
为property
属性,它返回一个FrozenList
(不可变列表),列表中存储每一级的label
对应的下标(也就是创建MultiIndex
时传入的labels
参数),以FrozenNDArray
的数据类型。.levels
为property
属性,它返回一个FrozenList
(不可变列表),列表中存储每一级的label
(也就是创建MultiIn
dex
时传入的levels
参数),以Index
的数据类型。
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import numpy as np import pandas as pd levels = [[ 'a' , 'b' ],[ 'c' , 'd' ]] labels = [[ 0 , 1 , 0 , 1 ],[ 0 , 0 , 1 , 1 ]] s = pd.MultiIndex(levels = levels,labels = labels,name = [ 'index1' , 'index2' ]) s # MultiIndex(levels=[['a', 'b'], ['c', 'd']], labels = [[ 0 , 1 , 0 , 1 ], [ 0 , 0 , 1 , 1 ]], names = [ 'index1' , 'index2' ]) s.values,s._values # (array([('a', 'c'), ('b', 'c'), ('a', 'd'), ('b', 'd')], dtype=object), array([( 'a' , 'c' ), ( 'b' , 'c' ), ( 'a' , 'd' ), ( 'b' , 'd' )], dtype = object )) v1 = s.values v2 = s._values v1.base is v2.base,v1 is v2,s._data # (True, True, None) s.labels,s.labels[ 0 ] # (FrozenList([[0, 1, 0, 1], [0, 0, 1, 1]]), FrozenNDArray([ 0 , 1 , 0 , 1 ], dtype = 'int8' )) s.levels,s.levels[ 0 ] # (FrozenList([['a', 'b'], ['c', 'd']]), Index([ 'a' , 'b' ], dtype = 'object' , name = 'index1' )) |
3.Seris
的结构
._name
为普通属性,返回Seris
的名字;.name
为property
属性,返回的也是Seris
名字.dtype/.dtypes
为property
属性,返回Series
的数据类型。.ftype/ftypes
为property
属性,返回一个字符串,说明Series
是否稀疏数据。(二者返回的字符串的值相同,但不是同一个字符串对象).values/._values
为property
属性,返回Series
的内部数据的视图.index
为普通属性,返回Series
的索引.shape
为property
属性,返回Series
的数据的形状._data
为普通属性,它返回的是一个SingleBlockManager
对象,该对象负责管理内部数据。SingleBlockManager
的.shape
属性为property
属性,返回内部数据的形状SingleBlockManager
的.blocks
属性为普通属性,返回一个列表,该列表只有一个元素,该元素为一个IntBlock
对象(或者其他的xxxBlock
对象),代表了内部数据。IntBlock
的.values
属性为普通属性,它返回内部数据:一个ndarray
。IntBlock
的.shape
属性为property
属性,它返回内部数据的形状
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | s = pd.Series({ 'a' : 1 , 'b' : 2 , 'c' : 3 },name = 'sr' ) s # a 1 b 2 c 3 Name: sr, dtype: int64 s.name,s._name,s.name is s._name # ('sr', 'sr', True) s.dtype,s.dtypes,s.dtype is s.dtypes # (dtype('int64'), dtype('int64'), True) s.ftype,s.ftypes,s.ftype is s.ftypes # ('int64:dense', 'int64:dense', False) f1,f2 = s.ftype,s.ftypes f1 is f2 # False s.values,s._values,s.values is s._values # (array([1, 2, 3], dtype=int64), array([1, 2, 3], dtype=int64), True) s.index,s.shape # (Index(['a', 'b', 'c'], dtype='object'), (3,)) s._data # SingleBlockManager Items: Index([ 'a' , 'b' , 'c' ], dtype = 'object' ) IntBlock: 3 dtype: int64 |
4.DataFrame
的结构
.index/columns
属性都为普通属性,它们返回的都是一个Index
对象,参考Series
。.dtypes
属性为property
属性,给出了每列的数值类型。它返回的是一个Series
。并且没有.dtype
属性,这一点与Series
不同。.ftypes
属性为property
属性,给出了每列是否为sparse/dense
的。它返回的是一个Series
。并且没有.ftype
属性,这一点与Series
不同。.values/._values/.shape
属性都为property
属性,参考Series
。._data
属性为普通属性,它返回的是一个BlockManager
对象,该对象负责管理内部数据。该对象的.block
属性(普通属性)返回一个列表,该列表里面有多个元素。DataFrame
尽量用一个数组保存类型相同的列。- 每个元素都为一个
xxBlock
对象。如IntBlock/FloatBlock...
- 一个
xxBlock
可能存储多个列的数据(这些列的数据都是同一个类型)
- 一个
xxBlock
对象的.values
属性(普通属性)就是存储的某个列(或者某些类型相同的列)的内部数据,一个ndarray
xxBlock
对象的.shape
属性(property
属性)就是存储的某个列(或者某些类型相同的列)的内部数据的形状.blocks
属性为property
属性。该属性返回一个字典,该字典的键为不同的数值类型,该字典的值为该数值类型的数值组成的DataFrame
举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import numpy as np import pandas as pd df = pd.DataFrame(np.arange( 0 , 12 ).reshape( 3 , 4 ),index = [ 'a' , 'b' , 'c' ],columns = [ 'c1' , 'c2' , 'c3' , 'c3' ]) df # c1 c2 c3 c3 a 0 1 2 3 b 4 5 6 7 c 8 9 10 11 df.index,df.columns # (Index(['a', 'b', 'c'], dtype='object'), Index([ 'c1' , 'c2' , 'c3' , 'c3' ], dtype = 'object' )) df.dtypes, type (df.dtypes) # (c1 int32 c2 int32 c3 int32 c3 int32 dtype: object , pandas.core.series.Series) df.ftypes, type (df.ftypes) # (c1 int32:dense c2 int32:dense c3 int32:dense c3 int32:dense dtype: object , pandas.core.series.Series) print (df.values,df._values) v1 = df.values v2 = df._values v1.base is v2.base # [[ 0 1 2 3] [ 4 5 6 7 ] [ 8 9 10 11 ]] [[ 0 1 2 3 ] [ 4 5 6 7 ] [ 8 9 10 11 ]] True manager = df._data manager #BlockManager Items: Index([ 'c1' , 'c2' , 'c3' , 'c3' ], dtype = 'object' ) Axis 1 : Index([ 'a' , 'b' , 'c' ], dtype = 'object' ) IntBlock: slice ( 0 , 4 , 1 ), 4 x 3 , dtype: int32 manager.blocks # (IntBlock: slice(0, 4, 1), 4 x 3, dtype: int32,) manager.blocks[ 0 ].values,manager.blocks[ 0 ].shape # (array([[ 0, 4, 8], [ 1 , 5 , 9 ], [ 2 , 6 , 10 ], [ 3 , 7 , 11 ]]), ( 4 , 3 )) df.blocks, type (df.blocks), type (df.blocks[ 'int32' ]) # ({'int32': c1 c2 c3 c3 a 0 1 2 3 b 4 5 6 7 c 8 9 10 11 }, dict , pandas.core.frame.DataFrame) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
2018-12-27 去掉python的警告