
3.1 数据对象
Pandas中最重要的两种数据对象是Series和DataFrame,其中DataFrame由多个Series组成,而索引是DataFrame和Series的重要组成部分,下面介绍它们的概念及基本用法。
3.1.1 Series对象
上一章介绍的Numpy多维数组常用于处理单一类型的数据,可看作列表的扩展;而Series可以管理多种类型的数据,可以通过索引值访问元素,更像基本数据类型中字典的扩展,可以把它视为带索引的一维数组。下面将从创建、查询、添加、删除等几方面学习Series的使用方法。
1.创建
创建Series需要指定值和索引,当不指定索引时,索引为元素的序号。

也可以使用转换的方式将其他类型的数据转换成Series类型。

2.查询
Series支持用索引值访问其中的数据,这种操作类似于访问字典元素;也可以用位置下标访问数据元素,操作方法类似于访问列表元素。

Series由两个数组组成,其数据值和索引值可作为属性访问。

Series还提供多维数组对象接口,用于处理多维数组的函数都可直接处理Series元素。

通过索引列表、下标列表、下标切片的方式可以访问Series中的一个或多个元素。

还可以通过Series的iteritems方法以迭代的方式遍历元素。

3.添加
用append方法连接两个已有的Series,并返回新的Series,且不改变原数据。

4.删除
用drop方法删除索引值对应的Series元素,并返回删除后的Series,且不改变原数据。

3.1.2 DataFrame对象
DataFrame类似于数据库中的数据表table,是数据处理中最常用的数据对象。从数据结构的角度可将其视为有标签的二维数组,横向为行,纵向为列,且每行有行索引,每列有列名,列中数据类型必须一致。
1.创建
利用转换方式将已有数据转换成DataFrame,其语法如下:

其中,data是待转换的数据,index是索引值(行),column是列名。下例通过数组组成的字典创建DataFrame。

在通过字典组成的数组创建DataFrame时,如果不指定索引,则以数据的序号作为索引,使用Series创建Dataframe与之同理。

通过数组创建DataFrame,用columns指定列名。

2.添加
用append函数可以在当前DataFrame的尾部添加一行,然后返回新表。添加的内容可以是列表、字典、Series,本例中以字典为例示范append函数的使用方法。

如果想在两行之间插入数据,则可以先用索引值将DataFrame切分成前后两个表,然后将前表、新行、后表连接在一起。
除了添加一行,append函数还支持将两个DataFrame表连接在一起,支持表连接的函数还有concat。下例中,将df表和其自身连接起来,使用ignore_index=True忽略索引值,索引值重新排序。

添加列最简单的方法是直接给新列赋值:

如果需要在指定位置插入新列,则需要用insert方法。

3.删除
用drop方法可以删除DataFrame的行和列。在删除列时,需要指定参数axis=1;当该参数默认为0时,即删除行。drop方法支持删除一行/多行或一列/多列,在删除行时需要指定行的索引值。在本例中,删除第1行后,仅剩第0行。

在删除列时需要指定列名,drop方法默认返回删除列后的数据表,原表不变。当指定其参数inplace=True时,原数据表内容被修改。

用del方法也可以从原表中删除a列。

还可以用pop方法删除列,调用pop方法之后,b列的内容作为函数返回值并同时从原表中删除。

3.1.3 Index对象
1.索引
DataFrame中的索引包括行索引和列索引,其类型为Pandas.Index,简称为pd.Index。它的结构类似于数组,但其数据内容不可以修改(不允许单个修改,但可以对行索引或列索引整体重新赋值)。在理论上,索引中允许内容重复,在数据表中允许有重名的列或者行索引值,但一般不推荐使用。

用pd.Index将其他类型转换成索引对象。

用values属性查看Index中的所有值。

用下标或下标数组读取部分索引值。

用get_loc或get_indexer查找值对应的下标。

2.修改索引
对DataFrame的column和index重新赋值可改变其索引,数据表内容不变。

如果不仅仅想改变索引值,还想重排行或列的顺序,可以使用DataFrame的reindex方法。从下列返回结果可以看到,reindex方法返回了新的数据表,原表不改变。对于已有的索引值,对应行的顺序发生了变化;对于不存在的索引值,生成了新的行并置为空值。

除了对行修改,reindex方法还支持修改列索引,用columns参数指定其新的列索引值。

用sort_index方法对索引重新排序,该方法默认返回新的DataFrame。

还有一种更为简单的方法,即用直接赋值的方法修改其列索引的顺序。

3.多重索引
多重索引包括多重行索引和多重列索引,在数据分析和建模过程中使用多重索引的情况并不多。多重列索引主要出现在从其他格式文件导入数据和导出数据,以及前期的数据处理过程中,如从Excel文件中导入的表格,如表3.1所示。
表3.1 Excel多重列索引数据

用read_excel方法读取数据表(读取Excel需要第三方库支持,具体方法请参见第5章),注意用header参数指定列索引包含前两行(读取双重行索引使用index_col=[0,1])。从返回结果可以看到,其每个字段被表示为多层列名组成的元组。

由于数据被解析成多重索引处理起来比较麻烦,因此一般会将其两列索引组合成单层索引。下例用join方法将元组连成的字符串作为新的字段名。

多重行索引常出现在groupby用多变量分组后的数据中(groupby将在3.3节中详细介绍,本例中代码的前三行只作为数据源使用,主要关注将索引转换为普通列的方法),在这种情况下,通常使用reset_index方法将多重行索引转换成普通列。创建多重行索引数据:

从运行结果可以看到,行索引为AGE和OWNRENT两层。在使用reset_index方法后,索引被转换为普通列。
