pandas系列学习(二):Series

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CoderPai/article/details/83309662

作者:chen_h
微信号 & QQ:862251340
微信公众号:coderpai


pandas系列学习(一):pandas入门

pandas系列学习(二):Series


简介

Pandas 是一个功能强大的工具包,为 Python 编程语言提供数据分析工具和结构。

pandas 提供的最重要的一个特性是 Series。在本文中,我们从初学者的角度介绍 Series 类。这意味着你无序了解有关 pandas 或者数据分析的任何信息即可从本教程理解 Series。

什么是 Series?

Series 类似于 Python 中的列表或者数组。它表示一系列的值(数字或者其他数据),例如一列数字。你可以把它想象成一个固定的 Python 列表。它体用了额外的功能,方法和操作符,使其成为列表的更强大版本。

要开始使用 Series,你需要将 pandas 包导入到 Python 程序中。这个也非常简单,我们只需要导入包就行了:

import pandas as pd	

3. 从列表创建 Series

现在让我们学习如何创建一个 Series。由于系列类似于列表,我们使用列表来创建 Series。

import pandas as pd

ser = pd.Series([1,3,5,7])
print(ser)
0    1
1    3
2    5
3    7
dtype: int64

请注意,打印 Series 时,会打印两列数字。第一列称为索引。它通常从 0 开始并一直运行到 N-1,其中 N 是Series 的大小。我们可以通过 ser.shape 来查看 Series 的大小。第二列是我们输入的实际数据。

4. 使用 Dict 初始化系列

也可以使用 Python 字典来初始化 Series。在这种情况下,Series 从 dict 的键中获取其索引,如本例所示:

prices = {'apple': 4.99,
         'banana': 1.99,
         'orange': 3.99,
         'grapes': 0.99}
ser = pd.Series(prices)
print(ser)
apple     4.99
banana    1.99
grapes    0.99
orange    3.99
dtype: float64

5. 从标量初始化 Series

你还可以使用标量来初始化 Series。在这种情况下,Series 的所有元素都初始化为相同的值。与标量一起使用进行初始化时,可以指定索引数组。在这种情况下,Series 的大小与索引数组的大小相同。

在下面的实例中,我们使用 range() 函数来指定索引(从而指定 Series 的大小)。

ser = pd.Series(2, index=range(0, 5))
print(ser)
0    2
1    2
2    2
3    2
4    2
dtype: int64

6. 创建 Series 的其他一些方法

以下是一些其他初始化的方法。

6.1 创建一个奇数序列

我们使用 range() 函数来创建一系列奇数。

ser = pd.Series(range(1, 10, 2))
print(ser)
0    1
1    3
2    5
3    7
4    9
dtype: int64

6.4 使用字母索引

ser = pd.Series(range(1, 15, 3), index=[x for x in 'abcde'])
print(ser)
a     1
b     4
c     7
d    10
e    13
dtype: int64

6.3 随机数序列

使用随机范围初始化系列:

import random

ser = pd.Series(random.sample(range(100), 6))
print(ser)
0    69
1    62
2    57
3    67
4    97
5     4
dtype: int64

6.4 结合列表

如果你有一个列表中的数据和另一个列表中的索引,有几种方法可以从中创建一个 Series。比如使用 dict。

x = dict(zip([x for x in 'abcdefg'], range(1, 8)))
print(x)
y = pd.Series(x)
print(y)
{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7}
a    1
b    2
c    3
d    4
e    5
f    6
g    7
dtype: int64

指定索引

跳过定义字典并直接创建 Series。

ser = pd.Series(range(1,8), index=[x for x in 'abcdefg'])
print(ser)
a    1
b    2
c    3
d    4
e    5
f    6
g    7
dtype: int64

7. 对 Series 进行命名

你还可以为 Series 指定名称。当在 DataFrame 的上下文中使用时,这个名称是可以作为列名的。

a = [1, 3, 5, 7]
print(pd.Series(a, name='joe'))
0    1
1    3
2    5
3    7
Name: joe, dtype: int64

8. 将列表与 Series 进行比较

Series 就像一个列表,我们可以按照列表的索引和切片来处理 Series。请注意,切片运算符返回的还是一个 Series 本身。

ser = pd.Series(random.sample(range(100), 10))
print(ser)
print('4th element: ', ser[4])
print('Slice: ', ser[3:8])
0    60
1     8
2    19
3    25
4    24
5    87
6    14
7    38
8     5
9    89
dtype: int64
4th element:  24
Slice:  3    25
4    24
5    87
6    14
7    38
dtype: int64

8.1 使用比较进行提取

虽然可以使用索引和切片提取列表元素,但也可以使用比较函数(返回 True 或者 False 的函数)提取 Series 元素,比如下面的例子:

x = random.sample(range(100), 10)
print(x)
y = pd.Series(x)
print(y)
print("===============")
print(y[y > 40])
[9, 71, 31, 83, 28, 40, 74, 58, 21, 42]
0     9
1    71
2    31
3    83
4    28
5    40
6    74
7    58
8    21
9    42
dtype: int64
===============
1    71
3    83
6    74
7    58
9    42
dtype: int64

8.2 使用列表索引

除了使用比较函数进行提取之外,还可以使用列表作为索引从 Series 中检索项目。

x = random.sample(range(100), 10)
print(x)
y = pd.Series(x)
print(y[[2, 0, 1, 2]])
[18, 3, 4, 70, 8, 79, 86, 82, 34, 53]
2     4
0    18
1     3
2     4
dtype: int64

8.3 利用函数来抽取 Series

与列表不同,我们可以使用 Series 调用接受标量参数的函数。返回的结果将是另一个 Series,其功能应用于 Series 中的每个元素。这允许更灵活和简介的方式来组合操作。

def joe(x): return x + 10
x = random.sample(range(100), 10)
print('Data => ', x, '\n')
y = pd.Series(x)
print('Applying pow => \n', pow(y, 2), '\n')
print('Applying joe => \n', joe(y))
Data =>  [25, 35, 54, 72, 12, 46, 74, 15, 93, 3] 

Applying pow => 
 0     625
1    1225
2    2916
3    5184
4     144
5    2116
6    5476
7     225
8    8649
9       9
dtype: int64 

Applying joe => 
 0     35
1     45
2     64
3     82
4     22
5     56
6     84
7     25
8    103
9     13
dtype: int64

9. 比较 dict 和 Series

Series 也表现的像 Python 字典一样。

9.1 使用标签进行索引

例如,你可以使用索引标签提取项目。

x = pd.Series(range(1,8), index=[x for x in 'abcdefg'])
print(x, '\n')
print('element "d" => ', x['d'])
a    1
b    2
c    3
d    4
e    5
f    6
g    7
dtype: int64 

element "d" =>  4

9.2 元素检查

使用 in 运算符检查 Series 是否包含特定标签。

x = pd.Series(range(1,8), index=[x for x in 'abcdefg'])
print(x, '\n')
print('is "j" in x?', 'j' in x)
print('is "d" in x?', 'd' in x)
a    1
b    2
c    3
d    4
e    5
f    6
g    7
dtype: int64 

is "j" in x? False
is "d" in x? True

猜你喜欢

转载自blog.csdn.net/CoderPai/article/details/83309662