数据分析和建模的大部分时间都用于数据准备和处理,即加载,清理和重新布置数据等。此外,由于使用了Python库,Pandas为无涯教程提供了高性能,灵活和高级的处理环境数据。Pandas可以使用各种函数来有效地处理数据。
为了增强数据处理的函数,必须使用一些索引来帮助基于标签对数据进行排序。因此,分层索引已成为图片,它被定义为Pandas的一项基本函数,可帮助使用多个索引级别。
在分层索引中,必须为数据创建多个索引。本示例创建具有多个索引的Series。
示例:
import pandas as pd info = pd.Series([11, 14, 17, 24, 19, 32, 34, 27], index = [['x', 'x', 'x', 'x', 'y', 'y', 'y', 'y'], ['obj1', 'obj2', 'obj3', 'obj4', 'obj1', 'obj2', 'obj3', 'obj4']]) data
输出:
aobj1 11 obj2 14 obj3 17 obj4 24 bobj1 19 obj2 32 obj3 34 obj4 27 dtype: int64
在这里采用了两个级别的索引,即(a,b)和(obj1,...,obj4),可以使用' index "命令。
info.index
输出:
MultiIndex(levels=[['x', 'y'], ['obj1', 'obj2', 'obj3', 'obj4']], labels=[[0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 2, 3, 0, 1, 2, 3]])
部分索引可以定义为从分层索引中选择特定索引的一种方式。下面的代码从数据中提取" b",
import pandas as pd info = pd.Series([11, 14, 17, 24, 19, 32, 34, 27], index = [['x', 'x', 'x', 'x', 'y', 'y', 'y', 'y'], ['obj1', 'obj2', 'obj3', 'obj4', 'obj1', 'obj2', 'obj3', 'obj4']]) info['b']
输出:
obj1 19 obj2 32 obj3 34 obj4 27 dtype: int64
此外,还可以基于内部级别即" obj"提取数据。以下结果定义了Series中'obj2'的两个可用值。
info[:, 'obj2']
输出:
x 14 y 32 dtype: int64
Unstack意味着将行标题更改为列标题。行索引将更改为列索引,因此Series将成为DataFrame。下面是取消堆叠数据的示例。
示例:
import pandas as pd info = pd.Series([11, 14, 17, 24, 19, 32, 34, 27], index = [['x', 'x', 'x', 'x', 'y', 'y', 'y', 'y'], ['obj1', 'obj2', 'obj3', 'obj4', 'obj1', 'obj2', 'obj3', 'obj4']]) # unstack on first level i.e. x, y #note that data row-labels are x and y data.unstack(0)
输出:
ab obj1 11 19 obj2 14 32 obj3 17 34 obj4 24 27 # unstack based on second level i.e. 'obj' info.unstack(1)
输出:
obj1 obj2 obj3 obj4 a 11 14 17 24 b 19 32 34 27
" stack()"操作用于将列索引转换为行索引。在上面的代码中,可以使用" stack "操作将" obj"作为列索引转换为行索引。
import pandas as pd info = pd.Series([11, 14, 17, 24, 19, 32, 34, 27], index = [['x', 'x', 'x', 'x', 'y', 'y', 'y', 'y'], ['obj1', 'obj2', 'obj3', 'obj4', 'obj1', 'obj2', 'obj3', 'obj4']]) # unstack on first level i.e. x, y #note that data row-labels are x and y data.unstack(0) d.stack()
输出:
aobj1 11 obj2 14 obj3 17 obj4 24 bobj1 19 obj2 32 obj3 34 obj4 27 dtype: int64
请记住,因为列索引需要二维数据,所以列索引仅适用于DataFrame(不适用于Series)。创建一个新的DataFrame来演示具有多个索引的列,
import numpy as np info = pd.DataFrame(np.arange(12).reshape(4, 3), index = [['a', 'a', 'b', 'b'], ['one', 'two', 'three', 'four']], columns = [['num1', 'num2', 'num3'], ['x', 'y', 'x']] ... ) info
输出:
num1 num2 num3 x y x a one0 1 2 two3 4 5 b three 6 7 8 four 9 10 11
# display row index info.index
输出:
MultiIndex(levels=[['x', 'y'], ['four', 'one', 'three', 'two']], labels=[[0, 0, 1, 1], [1, 3, 2, 0]])
# display column index info.columns
输出:
MultiIndex(levels=[['num1', 'num2', 'num3'], ['green', 'red']], labels=[[0, 1, 2], [1, 0, 1]])
可以使用" swaplevel "命令轻松地交换索引级别,该命令将输入作为两个级别编号。
import numpy as np info = pd.DataFrame(np.arange(12).reshape(4, 3), index = [['a', 'a', 'b', 'b'], ['one', 'two', 'three', 'four']], columns = [['num1', 'num2', 'num3'], ['x', 'y', 'x']] ... ) info.swaplevel('key1', 'key2') nnum1 num2 num3 p x y x key2 key1 onea 0 1 2 twoa 3 4 5 three b 6 7 8 four b 9 10 11
无涯教程可以使用" sort_index "命令对标签进行排序。数据将按" key2"名称(即按字母顺序排列的key2)排序。
info.sort_index(level='key2') nnum1 num2 num3 p x y x key1 key2 bfour 9 10 11 aone 0 1 2 bthree 6 7 8 atwo 3 4 5
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)