Pandas系列是一个类似于一维数组的对象,可以容纳任何类型的数据。你可以通过将字典传递给命令,从字典中创建一个pandas系列。pandas.Series().
在这篇文章中,你将了解到配置pandas.Series() 命令的不同方法,以便从字典中制作一个pandas系列,然后是一些实用的使用技巧。
使用pandas.Series方法
要从一个字典中制作一个系列,只需将字典传递给命令pandas.Series 方法。
字典的键构成系列的索引值,字典的值构成系列的值。
import pandas as pd
# Create the data of the series as a dictionary
ser_data = {'A': 5, 'B': 10, 'C': 15, 'D': 20, 'E': 25}
# Create the series
ser = pd.Series(ser_data)
ser
A 5
B 10
C 15
D 20
E 25
dtype: int64
系列的排序
默认情况下,系列的顺序与字典的键值相同。
然而,你可以使用索引参数改变系列值的顺序。
指定一个新的顺序
你可以直接指定索引的顺序为一个列表,系列将相应地被制作。
# Ordering a series
ser_data = {'C': 10, 'A': 5, 'D': 20, 'B': 15, 'E': 25}
# Define the new order of the series
ind = ['A', 'B', 'C', 'D', 'E']
# Create the series
ser = pd.Series(ser_data, index=ind)
ser
A 5
B 15
C 10
D 20
E 25
dtype: int64
使用sorted函数
sorted函数用于返回一个按升序或降序排序的可迭代对象。
你可以使用这个函数以升序或降序的方式排列系列的索引。
默认情况下,sorted函数将以升序排列数值。
# Ordering a series in ascending order
ser_data = {'C': 10, 'A': 5, 'D': 20, 'B': 15, 'E': 25}
# Create the series
ser = pd.Series(ser_data)
print('Initial ordering of the series:')
print(ser)
print('\n')
# ser_data.keys() is used to access the array of keys of the dictionary
asc_ser = pd.Series(ser_data, index=sorted(ser_data.keys()))
print('Series after arranging the indices in ascending order:')
print(asc_ser)
Initial ordering of the series:
C 10
A 5
D 20
B 15
E 25
dtype: int64
Series after arranging the indices in ascending order:
A 5
B 15
C 10
D 20
E 25
dtype: int64
对于以降序排列系列,请将布尔值True传递给sorted函数的参数reverse。
# Ordering a series in descending order
ser_data = {'C': 10, 'A': 5, 'D': 20, 'B': 15, 'E': 25}
# Create the series
ser = pd.Series(ser_data)
print('Initial ordering of the series:')
print(ser)
print('\n')
# Pass the boolean value True to sort the series in the descending order
desc_ser = pd.Series(ser_data, index=sorted(ser_data.keys(), reverse=True))
print('Series after arranging the indices in descending order:')
print(desc_ser)
Initial ordering of the series:
C 10
A 5
D 20
B 15
E 25
dtype: int64
Series after arranging the indices in descending order:
E 25
D 20
C 10
B 15
A 5
dtype: int64
对字典进行子集,形成具有特定值的系列
你可以通过使用索引参数对字典的值进行子集。指定字典的键值列表,你希望将其加入系列中。
# Subsetting the dictionary
# Create the data of the series as a dictionory
ser_data = {'A': 5, 'B': 10, 'C': 15, 'D': 20, 'E': 25}
# Specify the keys which are to be used to make the series
ser = pd.Series(ser_data, index=['B', 'C', 'D'])
ser
B 10
C 15
D 20
dtype: int64
使用一个用户定义的索引,有多余的索引
如果你传递的索引值列表中的标签多于字典中的值的数量,那么多余的标签的值将被视为NaN。
# Create the data of the series as a dictionory
ser_data = {'A': 5, 'B': 10, 'C': 15, 'D': 20, 'E': 25}
# Pass a list of index labels of length greater than the length of the dictionary
ind = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
# Create the series
ser = pd.Series(ser_data, index=ind)
ser
A 5.0
B 10.0
C 15.0
D 20.0
E 25.0
F NaN
G NaN
dtype: float64
实用提示
- 如果你向索引参数传递一个标签,而这个标签在字典中不存在,那么这个索引的值将被认为是NaN。
# Subsetting the dictionary
ser_data = {'A': 5, 'B': 10, 'C': 15, 'D': 20, 'E': 25}
# Specify the keys which are to be used to make the series
ser = pd.Series(ser_data, index=['B', 'C', 'H'])
ser
B 10.0
C 15.0
H NaN
dtype: float64
- 如果一个重复的索引标签被传递到索引参数中,那么对应的键的值也将被重复。
# Subsetting the dictionary
ser_data = {'A': 5, 'B': 10, 'C': 15, 'D': 20, 'E': 25}
# Specify the keys which are to be used to make the series
ser = pd.Series(ser_data, index=['A', 'B', 'C', 'D', 'E', 'A'])
ser
A 5
B 10
C 15
D 20
E 25
A 5
dtype: int64
测试你的知识
Q1: 在一个系列中不允许有重复的值作为索引值。真的还是假的?
答案是错。重复的值可以作为系列中的索引标签。
Q2: 如果没有指定索引参数,如何决定系列的索引标签?
答案是字典的键构成系列的索引。
使用下面的字典来回答下列问题。data={'q':10, 'w':20, 'e':30, 'r':40, 't':50, 'y':60}
Q3: 制作一个标签顺序相反的系列。
答案是 pandas.Series(data,index=['y','t','r','e','w','q'])
Q4: 制作一个只包含键 "q"、"r "和 "y "值的系列。
答案: pandas.Series(data, index=['q', 'r', 'y'])
Q5:制作一个数值按降序排列的系列。
答案: pandas.Series(data,index=sorted(data.keys(),reverse=True))