Python中的Series是一种一维数组,可以包含我们在pandas模块中指定的任何数据类型。唯一区别是pandas Series中的每个值都与一个索引相关联。默认的索引值是从0到数字-1,也可以指定自己的索引值。
以下一系列长示例可以帮助您理解此pandas Series,如何访问和修改其中的项、索引、列等。
Python pandas Series 示例
这是一个简单的pandas Series示例。如您所见,它已分配了从0到3(4-1)的默认索引值。
import pandas as pd from pandas import Series arr = Series([15, 35, 55, 75]) print(arr)
0 15
1 35
2 55
3 75
dtype: int64
Series 值和索引
Series有两个属性:values和index。您可以使用这些属性来获取有关它们的信息。
- values:此属性返回实际数据或其中的元素数组。
- index:顾名思义,此对象返回索引值。
arr = Series([12, 32, 52, -15, 122])
print(arr)
print('\nValues in this Array : ',arr.values)
print('Index Values of this Array : ',arr.index)

Python pandas Series 索引
您可以使用index属性创建自己的索引或为数据分配自己的索引。这是标识数据以便将来分析的便捷且最佳的方法。
arr = Series([25, 50, 75, 100, 125], index = [2, 4, 6, 8, 10])
print(arr)
print('\nValues in this Array : ',arr.values)
print('Index Values of this Array : ',arr.index)
Series 索引输出
2 25
4 50
6 75
8 100
10 125
dtype: int64
Values in this Array : [ 25 50 75 100 125]
Index Values of this Array : Int64Index([2, 4, 6, 8, 10], dtype='int64')
我将使用字母作为索引值。
import pandas as pd from pandas import Series arr = Series([2, 33, 66, 70, 15], index = ['a', 'e', 'i', 'o', 'u']) arr arr.index
如果使用外部文件保存此Series代码,请使用以下代码。
import pandas as pd
from pandas import Series
arr = Series([2, 33, 66, 70, 15], index = ['a', 'e', 'i', 'o', 'u'])
print(arr)
print('\nValues in this Array : ',arr.values)
print('Index Values of this Array : ',arr.index)
a 2
e 33
i 66
o 70
u 15
dtype: int64
Values in this Array : [ 2 33 66 70 15]
Index Values of this Array : Index(['a', 'e', 'i', 'o', 'u'], dtype='object')
使用arange创建Python Series
您还可以使用Numpy模块中可用的arange函数来创建从0到n-1的连续数字的pandas Series。
import pandas as pd import numpy as np from pandas import Series arr = Series(np.arange(6)) print(arr) # arr # Here, we are assigning the index names. arr1 = Series(np.arange(6), index = ['a', 'b', 'c', 'd', 'e', 'f']) print(arr1) #arr1
0 0
1 1
2 2
3 3
4 4
5 5
dtype: int64
a 0
b 1
c 2
d 3
e 4
f 5
dtype: int64
修改pandas Series 索引
index函数不仅允许您显示索引项,还可以进行修改。此示例更改实际的索引项,并将整数值作为索引。
import pandas as pd
from pandas import Series
new_arr = Series([2, 33, 66, 70, 15], index = ['a', 'e', 'i', 'o', 'u'])
print(new_arr)
print('\nValues in this Array : ',new_arr.values)
print('Index Values of this Array : ',new_arr.index)
print(' ')
# Assigning New Index Values
new_arr.index = [1, 2, 3, 4, 5]
print(new_arr)
print('\nValues in this Array : ',new_arr.values)
print('Index Values of this Array : ',new_arr.index)
修改索引输出
a 2
e 33
i 66
o 70
u 15
dtype: int64
Values in this Array : [ 2 33 66 70 15]
Index Values of this Array : Index(['a', 'e', 'i', 'o', 'u'], dtype='object')
1 2
2 33
3 66
4 70
5 15
dtype: int64
Values in this Array : [ 2 33 66 70 15]
Index Values of this Array : Int64Index([1, 2, 3, 4, 5], dtype='int64')
从字典创建Python Series
它还允许您为字典创建Series。当您使用字典时,值将成为Series数据,而字典键将充当索引。
import pandas as pd
from pandas import Series
f_dict = {'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries': 6000}
print('Dictionary Items')
print(f_dict)
arr = Series(f_dict)
print('\nArray Items')
print(arr)
字典到pandas Series的输出
Dictionary Items
{'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries': 6000}
Array Items
apples 500
kiwi 20
oranges 100
cherries 6000
dtype: int64
在将字典转换为Series时,我们还可以更改索引值的顺序。
import pandas as pd
from pandas import Series
f_dict = {'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries': 6000}
new_list = ['apples', 'cherries', 'kiwi', 'oranges']
arr = Series(f_dict, index = new_list)
print('Array Items')
print(arr)
转换字典并更改顺序的输出
Array Items
apples 500
cherries 6000
kiwi 20
oranges 100
dtype: int64
如果添加一个实际字典键中不存在的索引,那么它显然不会有任何与之关联的值。
import pandas as pd
from pandas import Series
f_dict = {'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries': 6000}
new_list = ['apples', 'banana', 'cherries', 'kiwi', 'oranges']
arr = Series(f_dict, index = new_list)
print('Array Items')
print(arr)
从下面的对象屏幕截图可以看出,它为该索引或键返回了NaN作为值。
Array Items
apples 500.0
banana NaN
cherries 6000.0
kiwi 20.0
oranges 100.0
dtype: float64
Series 属性
Python Series 对象有一个名为name的重要属性。您可以使用此属性为数据和索引分配名称。
import pandas as pd
from pandas import Series
f_dict = {'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries': 6000}
arr = Series(f_dict)
print(arr)
print('\nAssigning Names')
arr.name = 'No of Items'
arr.index.name = 'Fruits'
print(arr)
属性输出
apples 500
kiwi 20
oranges 100
cherries 6000
dtype: int64
Assigning Names
Fruits
apples 500
kiwi 20
oranges 100
cherries 6000
Name: No of Items, dtype: int64
访问Series项
与numpy数组一样,我们可以使用索引位置或索引编号来访问其中的一个项目。
import pandas as pd from pandas import Series arr = Series([22, 44, 66, 88, 108]) arr
访问索引位置0、2和4处的项。
arr[0] arr[2] arr[4]
从现有Series创建新的或选择一组值。
arr[[1, 3, 0, 4]]
访问项输出
>>> import pandas as pd
>>> from pandas import Series
>>> arr = Series([22, 44, 66, 88, 108])
>>> arr
0 22
1 44
2 66
3 88
4 108
dtype: int64
>>> arr[0]
22
>>> arr[2]
66
>>> arr[4]
108
>>> arr[[1, 3, 0, 4]]
1 44
3 88
0 22
4 108
dtype: int64
>>>
我们使用自定义索引值来访问其中的数据。为此,我们使用了字母字符作为索引项。
import pandas as pd from pandas import Series arr = Series([2, 4, -6, 8, -10, 12], index = ['a', 'e', 'i', 'o', 'u', 'z']) arr arr['a'] arr['u'] arr[['a', 'o', 'z', 'e']]
>>> import pandas as pd
>>> from pandas import Series
>>> arr = Series([2, 4, -6, 8, -10, 12], index = ['a', 'e', 'i', 'o', 'u', 'z'])
>>> arr
a 2
e 4
i -6
o 8
u -10
z 12
dtype: int64
>>> arr['a']
2
>>> arr['u']
-10
>>> arr[['a', 'o', 'z', 'e']]
a 2
o 8
z 12
e 4
dtype: int64
Python pandas Series in 运算符
在对数据执行任何操作之前,我们最好知道要查找的索引是否存在。为此,我们的in运算符演示可以检查并返回True(如果存在),否则返回False。
import pandas as pd from pandas import Series arr1 = Series([22, 33, 44, 55], index = ['a', 'b', 'c', 'd']) arr1 'b' in arr1 'c' in arr1 'f' in arr1
>>> import pandas as pd
>>> from pandas import Series
>>> arr1 = Series([22, 33, 44, 55], index = ['a', 'b', 'c', 'd'])
>>> arr1
a 22
b 33
c 44
d 55
dtype: int64
>>> 'b' in arr1
True
>>> 'c' in arr1
True
>>> 'f' in arr1
False
我将使用字典作为数组输入并使用in运算符。
import pandas as pd
from pandas import Series
f_dict = {'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries':6000}
arr2 = Series(f_dict)
arr2
'kiwi' in arr2
'banana' in arr2
'oranges' in arr2
against it的in运算符输出
>>> import pandas as pd
>>> from pandas import Series
>>> f_dict = {'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries':6000}
>>> arr2 = Series(f_dict)
>>> arr2
apples 500
kiwi 20
oranges 100
cherries 6000
dtype: int64
>>> 'kiwi' in arr2
True
>>> 'banana' in arr2
False
>>> 'oranges' in arr2
True
pandas Series Null值
Python pandas模块使用isnull和notnull函数来识别Null值并返回布尔值True或False。为了演示isnull和notnull函数,我们使用了上面使用的相同字典。
import pandas as pd
from pandas import Series
dict_items = {'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries': 6000}
f_list = ['apples', 'banana', 'cherries', 'kiwi', 'oranges']
arr = Series(dict_items, index = f_list)
arr
如果它是NULL或NA,则返回True,否则返回False。
pd.isnull(arr) arr.isnull()
如果值为NaN、Null或NA,则返回False,否则返回True。
pd.notnull(arr) arr.notnull()
pandas isnull、notnull输出
>>> import pandas as pd
>>> from pandas import Series
>>> dict_items = {'apples': 500, 'kiwi': 20, 'oranges': 100, 'cherries': 6000}
>>> f_list = ['apples', 'banana', 'cherries', 'kiwi', 'oranges']
>>> arr = Series(dict_items, index = f_list)
>>> arr
apples 500.0
banana NaN
cherries 6000.0
kiwi 20.0
oranges 100.0
dtype: float64
>>> pd.isnull(arr)
apples False
banana True
cherries False
kiwi False
oranges False
dtype: bool
>>> arr.isnull()
apples False
banana True
cherries False
kiwi False
oranges False
dtype: bool
>>> pd.notnull(arr)
apples True
banana False
cherries True
kiwi True
oranges True
dtype: bool
>>> arr.notnull()
apples True
banana False
cherries True
kiwi True
oranges True
dtype: bool
Python pandas Series 算术运算
pandas Series允许您对其数据执行算术运算。您可以使用任何运算符对所有项执行操作。此示例向您展示了pandas Series的算术运算。
import pandas as pd from pandas import Series arr = Series([2, 4, -6, 8, -7], index = ['a', 'e', 'i', 'o', 'u']) arr
向其中的每个项加3。
arr + 3
从各项中减去2。
arr - 2
我们将每一项乘以10。
arr * 10
返回其中值大于0的子集。
arr[arr > 0]
算术运算输出
>>> import pandas as pd
>>> from pandas import Series
>>> arr = Series([2, 4, -6, 8, -7], index = ['a', 'e', 'i', 'o', 'u'])
>>> arr
a 2
e 4
i -6
o 8
u -7
dtype: int64
>>> arr + 3
a 5
e 7
i -3
o 11
u -4
dtype: int64
>>> arr - 2
a 0
e 2
i -8
o 6
u -9
dtype: int64
>>> arr * 10
a 20
e 40
i -60
o 80
u -70
dtype: int64
>>> arr[arr > 0]
a 2
e 4
o 8
dtype: int64
Series数学函数
您可以使用numpy模块支持的数学函数对其进行操作。
from pandas import Series import numpy as np arr = Series([2, 4, -6, 8, -7], index = ['a', 'e', 'i', 'o', 'u']) arr
计算e的幂
np.exp(arr)
绝对正值
np.fabs(arr)
其中每个项的平方根
np.sqrt(arr)
pandas上的数学函数
>>> import pandas as pd
>>> from pandas import Series
>>> import numpy as np
>>> arr = Series([2, 4, -6, 8, -7], index = ['a', 'e', 'i', 'o', 'u'])
>>> arr
a 2
e 4
i -6
o 8
u -7
dtype: int64
>>> np.exp(arr)
a 7.389056
e 54.598150
i 0.002479
o 2980.957987
u 0.000912
dtype: float64
>>> np.fabs(arr)
a 2.0
e 4.0
i 6.0
o 8.0
u 7.0
dtype: float64
>>> np.sqrt(arr)
a 1.414214
e 2.000000
i NaN
o 2.828427
u NaN
dtype: float64