个性化阅读
专注于IT技术分析

Pandas数据处理解析和例子

点击下载

数据分析和建模的大部分时间都花费在数据准备和处理上, 即加载, 清理和重新布置数据等。此外, 由于有了Python库, Pandas为我们提供了高性能, 灵活和高级的处理环境数据。大Pandas可以使用各种功能来有效地处理数据。

分层索引

为了增强数据处理的功能, 我们必须使用一些索引来帮助根据标签对数据进行排序。因此, 分层索引已成为图片, 它被定义为Pandas的一项基本功能, 可帮助我们使用多个索引级别。

创建多个索引

在分层索引中, 我们必须为数据创建多个索引。本示例创建具有多个索引的系列。

例:

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”提取数据。以下结果定义了系列中’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()’操作用于将列索引转换为行索引。在上面的代码中, 我们可以使用”堆栈”操作将” 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

赞(0)
未经允许不得转载:srcmini » Pandas数据处理解析和例子

评论 抢沙发

评论前必须登录!