3-Pandas Series结构

Series 结构

Series 结构,也称 Series 序列,是 Pandas 常用的数据结构之一,它是一种类似于一维数组的结构,由一组数据值(value)和一组标签组成,其中标签与数据值之间是一一对应的关系。

Series 可以保存任何数据类型,比如整数、字符串、浮点数、Python 对象等,它的标签默认为整数,从 0 开始依次递增。Series 的结构图,如下所示:

pandas series

通过标签我们可以更加直观地查看数据所在的索引位置。

创建Series对象

Pandas 使用 Series() 函数来创建 Series 对象,通过这个对象可以调用相应的方法和属性,从而达到处理数据的目的:

import pandas as pd
s=pd.Series( data, index, dtype, copy)

参数说明如下所示:

参数名称描述
data输入的数据,可以是列表、常量、ndarray 数组等。
index索引值必须是惟一的,如果没有传递索引,则默认为 np.arrange(n)。
dtypedtype表示数据类型,如果没有提供,则会自动判断得出。
copy表示对 data 进行拷贝,默认为 False。

我们也可以使用数组、字典、标量值或者 Python 对象来创建 Series 对象。下面展示了创建 Series 对象的不同方法:

1) 创建一个空Series对象

使用以下方法可以创建一个空的 Series 对象,如下所示:

import pandas as pd
#输出数据为空
s = pd.Series()
print(s)

输出结果如下:

Series([], dtype: float64)
2) ndarray创建Series对象

ndarray 是 NumPy 中的数组类型,当 data 是 ndarry 时,传递的索引必须具有与数组相同的长度。假如没有给 index 参数传参,在默认情况下,索引值将使用是 range(n) 生成,其中 n 代表数组长度,如下所示:

[0,1,2,3…. range(len(array))-1]

使用默认索引,创建 Series 序列对象:

import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data)
print (s)

输出结果如下:

0    a
1    b
2    c
3    d
dtype: object

上述示例中没有传递任何索引,所以索引默认从 0 开始分配 ,其索引范围为 0 到len(data)-1,即 0 到 3。这种设置方式被称为“隐式索引”。

除了上述方法外,你也可以使用“显式索引”的方法定义索引标签,示例如下:

import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
#自定义索引标签(即显示索引)
s = pd.Series(data,index=[100,101,102,103])
print(s)

输出结果:

100    a
101    b
102    c
103    d
dtype: object
3) dict创建Series对象

您可以把 dict 作为输入数据。如果没有传入索引时会按照字典的键来构造索引;反之,当传递了索引时需要将索引标签与字典中的值一一对应。

下面两组示例分别对上述两种情况做了演示。

示例1,没有传递索引时:

import pandas as pd
import numpy as np
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd.Series(data)
print(s)

输出结果:

a    0.0
b    1.0
c    2.0
dtype: float64

示例 2,为index参数传递索引时:

import pandas as pd
import numpy as np
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd.Series(data,index=['b','c','d','a'])
print(s)

输出结果:

b    1.0
c    2.0
d    NaN
a    0.0
dtype: float64

当传递的索引值无法找到与其对应的值时,使用 NaN(非数字)填充。

4) 标量创建Series对象

如果 data 是标量值,则必须提供索引,示例如下:

import pandas as pd
import numpy as np
s = pd.Series(5, index=[0, 1, 2, 3])
print(s)

输出如下:

0    5
1    5
2    5
3    5
dtype: int64

标量值按照 index 的数量进行重复,并与其一一对应。

访问Series数据

上述讲解了创建 Series 对象的多种方式,那么我们应该如何访问 Series 序列中元素呢?分为两种方式,一种是位置索引访问;另一种是索引标签访问。

1) 位置索引访问

这种访问方式与 ndarray 和 list 相同,使用元素自身的下标进行访问。我们知道数组的索引计数从 0 开始,这表示第一个元素存储在第 0 个索引位置上,以此类推,就可以获得 Series 序列中的每个元素。下面看一组简单的示例:

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print("位置下标访问:",s[0])  #位置下标
print("标签下标访问:",s['a']) #标签下标

输出结果:

位置下标访问: 1
标签下标访问: 1

通过切片的方式访问 Series 序列中的数据,示例如下:

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print(s[:3])

输出结果:

a    1
b    2
c    3
dtype: int64

如果想要获取最后三个元素,也可以使用下面的方式:

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print(s[-3:])

输出结果:

c    3
d    4
e    5
dtype: int64
2) 索引标签访问

Series 类似于固定大小的 dict,把 index 中的索引标签当做 key,而把 Series 序列中的元素值当做 value,然后通过 index 索引标签来访问或者修改元素值。

示例1,使用索标签访问单个元素值:

import pandas as pd
s = pd.Series([6,7,8,9,10],index = ['a','b','c','d','e'])
print(s['a'])

输出结果:

6

示例 2,使用索引标签访问多个元素值

import pandas as pd
s = pd.Series([6,7,8,9,10],index = ['a','b','c','d','e'])
print(s[['a','c','d']])

输出结果:

a    6
c    8
d    9
dtype: int64

示例3,如果使用了 index 中不包含的标签,则会触发异常:

import pandas as pd
s = pd.Series([6,7,8,9,10],index = ['a','b','c','d','e'])
#不包含f值
print(s['f'])

输出结果:

......
KeyError: 'f'

Series常用属性

下面我们介绍 Series 的常用属性和方法。在下表列出了 Series 对象的常用属性。

名称属性
axes以列表的形式返回所有行索引标签。
dtype返回对象的数据类型。
empty返回一个空的 Series 对象。
ndim返回输入数据的维数。
size返回输入数据的元素数量。
values以 ndarray 的形式返回 Series 对象。
index返回一个RangeIndex对象,用来描述索引的取值范围。

现在创建一个 Series 对象,并演示如何使用上述表格中的属性。如下所示:

import pandas as pd
import numpy as np
s = pd.Series(np.random.randn(5))
print(s)

输出结果:

0   -2.922241
1    0.859586
2    1.037070
3   -2.395312
4    1.611217
dtype: float64

上述示例的行索引标签是 [0,1,2,3,4]。

1) axes
import pandas as pd
import numpy as np
s = pd.Series(np.random.randn(5))
print("The axes are:\n",s.axes)

输出结果

The axes are:
 [RangeIndex(start=0, stop=5, step=1)]
2) dtype
import pandas as pd
import numpy as np
s = pd.Series(np.random.randn(5))
print("The dtype is::\n",s.dtype)

输出结果:

The dtype is::
 float64
3) empty

返回一个布尔值,用于判断数据对象是否为空。示例如下:

import pandas as pd
import numpy as np
s = pd.Series(np.random.randn(5))
print ("是否为空对象?\n",s.empty)

输出结果:

是否为空对象?
False
4) ndim

查看序列的维数。根据定义,Series 是一维数据结构,因此它始终返回 1。

import pandas as pd
import numpy as np
s = pd.Series(np.random.randn(5))
print (s)
print ("Series的维度:",s.ndim)

输出结果:

0   -0.367340
1    0.333361
2   -1.062686
3    0.534339
4   -1.136869
dtype: float64
Series的维度: 1
5) size

返回 Series 对象的大小(长度)。

import pandas as pd
import numpy as np
s = pd.Series(np.random.randn(5))
print (s)
print ("Series的元素个数:",s.size)

输出结果:

0   -1.160195
1   -0.805764
2   -0.488558
3   -0.258666
4    1.157489
dtype: float64
Series的元素个数: 5
6) values

以数组的形式返回 Series 对象中的数据。

import pandas as pd
import numpy as np
s = pd.Series(np.random.randn(5))
print (s)
print("输出series中数据:\n",s.values)

输出结果:

0   -0.342189
1   -0.088567
2    1.436252
3    0.119682
4   -0.868701
dtype: float64
输出series中数据:
 [-0.34218891 -0.08856744  1.43625244  0.11968184 -0.86870134]
7) index

该属性用来查看 Series 中索引的取值范围。示例如下:

#显示索引
import pandas as pd
s=pd.Series([1,2,5,8],index=['a','b','c','d'])
print("显示索引:\n",s.index)
#隐式索引
s1=pd.Series([1,2,5,8])
print("隐式索引:\n",s1.index)

输出结果:

显示索引:
 Index(['a', 'b', 'c', 'd'], dtype='object')
隐式索引:
 RangeIndex(start=0, stop=4, step=1)

Series常用方法

1) head()&tail()查看数据

如果想要查看 Series 的某一部分数据,可以使用 head() 或者 tail() 方法。其中 head() 返回前 n 行数据,默认显示前 5 行数据。示例如下:

import pandas as pd
import numpy as np

s = pd.Series(np.random.randn(5))

print("The original series is:\n", s)

print("前三行数据:\n", s.head(3))

输出结果:

 0   -1.069508
1    0.465605
2   -1.787956
3   -0.457717
4    0.284744
dtype: float64
前三行数据:
 0   -1.069508
1    0.465605
2   -1.787956
dtype: float64

tail() 返回的是后 n 行数据,默认为后 5 行。示例如下:

import pandas as pd
import numpy as np

s = pd.Series(np.random.randn(5))

print("The original series is:\n", s)

print("后三行数据:\n", s.tail(3))

输出结果:

 0   -0.285554
1    1.270533
2    1.287082
3   -0.933982
4    0.672372
dtype: float64
后三行数据:
 2    1.287082
3   -0.933982
4    0.672372
dtype: float64

2) isnull()&nonull()检测缺失值

isnull() 和 nonull() 用于检测 Series 中的缺失值。所谓缺失值,顾名思义就是值不存在、丢失、缺少。

  • isnull():如果为值不存在或者缺失,则返回 True。
  • notnull():如果值不存在或者缺失,则返回 False。

其实不难理解,在实际的数据分析任物中,数据的收集往往要经历一个繁琐的过程。在这个过程中难免会因为一些不可抗力,或者人为因素导致数据丢失的现象。这时,我们可以使用相应的方法对缺失值进行处理,比如均值插值、数据补齐等方法。上述两个方法就是帮助我们检测是否存在缺失值。示例如下:

import pandas as pd
#None代表缺失数据
s=pd.Series([1,2,5,None])
print(pd.isnull(s))  #是空值返回True
print(pd.notnull(s)) #空值返回False

输出结果:

0    False
1    False
2    False
3     True
dtype: bool
0     True
1     True
2     True
3    False
dtype: bool
失值

isnull() 和 nonull() 用于检测 Series 中的缺失值。所谓缺失值,顾名思义就是值不存在、丢失、缺少。

- isnull():如果为值不存在或者缺失,则返回 True。
- notnull():如果值不存在或者缺失,则返回 False。


其实不难理解,在实际的数据分析任物中,数据的收集往往要经历一个繁琐的过程。在这个过程中难免会因为一些不可抗力,或者人为因素导致数据丢失的现象。这时,我们可以使用相应的方法对缺失值进行处理,比如均值插值、数据补齐等方法。上述两个方法就是帮助我们检测是否存在缺失值。示例如下:

```python
import pandas as pd
#None代表缺失数据
s=pd.Series([1,2,5,None])
print(pd.isnull(s))  #是空值返回True
print(pd.notnull(s)) #空值返回False

输出结果:

0    False
1    False
2    False
3     True
dtype: bool
0     True
1     True
2     True
3    False
dtype: bool
  • 20
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值