Series
概述
- Series也是Pandas中的最基本的数据结构对象,也是DataFrame的列对象或者行对象,series本身也具有行索引。
- Series是一种类似于一维数组的对象,由下面两个部分组成:
- values:一组数据(numpy.ndarray类型)
- index:相关的数据行索引标签;如果没有为数据指定索引,于是会自动创建一个0到N-1(N为数据的长度)的整数型索引。
(1) Series的常用属性
-
常见属性
属性 说明 loc 使用索引值取子集 iloc 使用索引位置取子集 dtype或dtypes Series内容的类型 T Series的转置矩阵 shape 数据的维数 size Series中元素的数量 values Series的值 index Series的索引值 - loc属性
first_row = data.loc[941] print(first_row) # 获取第一行数据, 但是是以列的方式展示的 print(type(first_row)) # <class 'pandas.core.series.Series'>
- iloc属性
first_row = data.iloc[0] # 使用索引位置获取自己 print(first_row) # 获取第一行数据, 但是是以列的方式展示的 print(type(first_row)) # <class 'pandas.core.series.Series'>
- dtype 或 dtypes
print(first_row.dtype) # 打印Series的元素类型, object表示字符串 print(first_row['year'].dtype) # 打印Series的year列的元素类型, int64 # 打印Series的year列的元素类型, 该列值为字符串, 字符串没有dtype属性, 所以报错. print(first_row['firstname'].dtype)
- shape 和 size属性
print(first_row.shape) # 维度 # 结果为: (7,) 因为有7列元素 print(first_row