首页 / 冬装搭配 / python多层索引,python层次结构

python多层索引,python层次结构

“目录”

数据整理:聚合、合并和重塑

数据整理: 组合、组合、重塑

8.1

=层次索引

8.2

=合并数据集

8.3

=成形和轴旋转

第七章终于结束了,第八章即将开始。干杯[小掌声]!

我省略了原书第7章中其余的正则化部分。因为关于正则化其实还有很多东西,但是原著只解释了一点,并没有重点介绍。因为作者认为正则化不是正则化。如果你专注于数据分析,跳过它们会变得很痛苦!

本说明主要描述分层索引,包括以下新方法:

DataFrame.swaplevel()

DataFrame.sum()

DataFrame.sort_index()

DataFrame.reset_index()

分层索引分层索引是pandas 的一个关键功能,它允许您:

轴上有多个索引级别。

首先,我们先来了解一下什么是层次索引。

[1]: 将pandas 导入为pdIn [2]: 将numpy 导入为npIn [3]: data=pd.Series(np.random.randn(9),index=[[\'a\', \'a\', \'a \' , \'b\', \'b\', \'c\', \'c\', \'d\', \'d\'], [1, 2, 3, 1, 3, 1, 2, 2, 3]]) 在[ 4 中]: 数据输出[4]:a 1 0.740915 2 -1.604666 3 1.236358b 1 0.307977 3 1.856143c 1 -0.243549 2 -0.924816d 2 -0.183851 3 -0.104385 如果检查dtype : float64 数据的索引属性,这就是MultiIndex 。级别中的第一个索引是主索引,第二个索引是辅助索引。看一下代码。

[5]: data.indexOut[5]:MultiIndex(level=[[\'a\', \'b\', \'c\', \'d\'], [1, 2, 3]], 代码=[[0, 0] , 0, 1, 1, 2, 2, 3, 3], [0, 1, 2, 0, 2, 0, 1, 1, 2]]) 选择索引为“b”的子集。

在[6]: data[\'b\']Out[6]:1 0.3079773 1.856143dtype: float64 选择索引从\'b\' 到\'c\' 的这个子集。

在[7]: data[\'b\':\'c\']Out[7]:b 1 0.307977 3 1.856143c 1 -0.243549 2 -0.924816dtype: float64 选择索引为\'b\' 和\'d\' 的子集。

[8]: data.loc[[\'b\', \'d\']]Out[8]:b 1 0.307977 3 1.856143d 2 -0.183851 3 -0.104385dtype: 对于float64,也可以这样选择。选择二级索引如下: “2”的子集:

在[9]: data.loc[: 2]Out[9]:a -1.604666c -0.924816d -0.183851dtype: 如果你熟悉float64 切片,上面选择子集的方法想必也不难理解。

对于DataFrame,每个轴都可以有一个分层索引。

[13]: 帧=pd.DataFrame(np.arange(12).reshape((4,3)), index=[[\'a\',\'a\',\'b\',\'b\'],[1 , 2,1,2]],列=[[\'俄亥俄\',\'俄亥俄\',\'科罗拉多\'],[\'绿色\',\'红色\',\'绿色\']])in[14]: 帧输出[14] : Ohio Colorado Green Red Greena 1 0 1 2 2 3 4 5b 1 6 7 8 2 9 1011您可以为每个图层设置名称。

[15]:frame.index name=[\'key1\',\'key2\'][16]:frame.columns.names=[\'state\',\'color\'][17]:frameout[17]:state俄亥俄州科罗拉多州颜色绿色红色Greenkey1 key2a 1 0 1 2 2 3 4 5b 1 6 7 8 2 9 1011 子集(分组)选择与之前没有太大不同。

In [18]: Frame[\'Ohio\']Out[18]:color Green Redkey1 key2a 1 0 1 2 3 4b 1 6 7 2 9 10In [19]: Frame[\'Ohio\'][\'Red\']Out[19] :key1 key2a 1 1 2 4b 1 7 210Name: 红色,dtype: int32In [22]: 帧[\'Ohio\'][\'Red\'][\'a\']Out[22] :key21 12 4Name: 红色,dtype: int32In [23]3336 0帧[ \'Ohio\'][\'Red\'][\'a\'][1]Out[23]: 1 使用loc 函数选择:

In [24]: Frame.loc[\'a\', \'Ohio\']Out[24]:color Green Redkey21 0 12 3 4In [25]: Frame.loc[\'a\', [\'Ohio\', \'Colorado\']] Out[25]: 州俄亥俄州科罗拉多州颜色绿色红色绿色键21 0 1 22 3 4 5

swaplevel 有时您需要对轴上的级别重新排序或根据指定级别的值对数据进行排序。

交换水平

接受两个级别编号或名称并返回一个级别交换的新对象。

然而,数据保持不变。

[24]: Frame.swaplevel(\'key1\', \'key2\')Out[24]:state 俄亥俄州Coloradocolor 绿红Greenkey2 key11 a 0 1 22 a 3 4 51 b 6 7 82 b 9 1011sort_index 为:

根据各个级别值对数据进行排序

例如,以下按“key2”级别对数据进行排序。

在[25]: Frame.sort_index(level=1)Out[25]:state Ohio Coloradocolor Green Red Greenkey1 key2a 1 0 1 2b 1 6 7 8a 2 3 4 5b 2 9 1011swaplevel 可以与sort_index 一起使用。下单后,系统会按照指定级别进行排序。

在[26]: Frame.swaplevel(0, 1).sort_index(level=0)Out[26]:state 俄亥俄州Coloradocolor 绿红Greenkey2 key11 a 0 1 2 b 6 7 82 a 3 4 5 b 9 1011

汇总统计的求和求和方法已在前面的注释中描述,但还有另一种求和方法。

level 选项用于指定要求和的轴上的级别。

在[27]: FrameOut[27]:state 俄亥俄州Coloradocolor 绿色Red Greenkey1 key2a 1 0 1 2 2 3 4 5b 1 6 7 8 2 9 1011In [28]: Frame.sum(level=\'key2\')Out[28]:state俄亥俄州科罗拉多州颜色绿色红色Greenkey21 6 8102 12 1416 轴=1,级别=\'颜色\'总计:

In [29]: Frame.sum(level=\'color\', axis=1)Out[29]:color 绿红key1 key2a 1 2 1 2 8 4b 1 14 7 2 20 10

使用DataFrame 列进行索引您还可以使用DataFrame 列作为行索引。

首先创建一个数据框。

[30]: 帧=pd.DataFrame({\'a\':range(7), \'b\':range(7, 0, -1), \'c\':[\'一\', \'一\', \'一\', \'二\', \'二\', \'二\', \'二\'], \'d\':[0,1,2,0,1,2,3]})In [31]: FrameOut[31]: a b c d0 0 7 一01 1 6 一12 2 5 一23 3 4 二04 4 3 二15 5 2 二26 6 1 二3DataFrame 的set_index 函数如下所示:

将一列或多列转换为行索引。

单击以创建新的DataFrame。

In [32]: Frame 2=Frame.set_index([\'c\', \'d\'])In [33]: Frame 2Out[33]:a bc 完成0 0 7 1 1 6 2 2 5two 0 3 4 1 4 3 2 5 2 3 6 1 默认情况下,转换为行索引的列将被删除,但您可以使用drop=False 保留它们。

[34]: 与frame.set_index([\'c\', \'d\'], drop=False)Out[34]:a b c dc 完成0 0 7 一0 1 1 6 一1 2 2 5 一2two 0 3 4 二0 1 4 3 二1 2 5 2 二2 3 6 1 二3reset_index 函数是set_index 的逆函数。

在[35]: Frame2.reset_index()Out[35]: c d a b0 一0 0 71 一1 1 62 一2 2 53 二0 3 44 二1 4 35 二2 5 26 二3 6 1好了,就这样了。

再见,再见!

-结尾-

本文来自网络,不代表服装搭配_服装搭配的技巧_衣服的穿配法_服装搭配网立场,转载请注明出处:https://www.fzdapei.com/335112.html
上一篇
下一篇

为您推荐

返回顶部