python里有列表,很强大,pandas里的Series更强大,它是dataframe的基石。
它可以表示表格中的一列数据(理论上不太严谨,表格里的一行也是Series),相当于一维数组,它最强大的地方在于它可以通过标签快速找到它,也称为索引。
import pandas as pd
# 语文,数学,英语
score = pd.Series([99, 120, 114])
print(score)
输出:
0 99
1 120
2 114
dtype: int64
三科的成绩可以用不同的标签来索引它。
import pandas as pd
# 语文,数学,英语
score = pd.Series([99, 120, 114], index=['cn', 'math', 'en'])
print(score)
输出:
cn 99
math 120
en 114
dtype: int64
Series里的所有数据的类型必须是一致的,构建的时候系统会自动猜出一种数据类型,上面的例子里是int64,但成绩可能是小数,所以可以指定dtype参数。
这里需要导入numpy,用到np.float32。
import pandas as pd
import numpy as np
# 语文,数学,英语
score = pd.Series([99, 120, 114], index=['cn', 'math', 'en'], dtype=np.float32)
print(score)
输出:
cn 99.0
math 120.0
en 114.0
dtype: float32
如果直接用小数构建Series,则dtype类型为float64。
score = pd.Series([99.0, 120.0, 114.0], index=['cn', 'math', 'en'])
输出:
cn 99.0
math 120.0
en 114.0
dtype: float64
与python里的列表一样。可以根据位置访问,还可以根据标签访问。
print(score[0])
print(score['cn'])
print(score['math'])
print(score['en'])
字典里的key就是索引,value就是Series的值。
score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
需要注意的是:字典里的key是不允许重复的,但Series里的索引可以重复。
score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
print(score.index)
print(score.values)
输出:
Index(['cn', 'math', 'en'], dtype='object')
[ 99. 120. 114.]
arr = np.random.random(size=10)
ser = pd.Series(arr)
print(ser)
输出:
0 0.112506
1 0.507774
2 0.732660
3 0.480606
4 0.944777
5 0.777633
6 0.682351
7 0.011652
8 0.893980
9 0.971912
dtype: float64
可以像python里根据位置来切片,注意范围是开区间。
也可以用标签来切片,这里是闭区间,即冒号前后的元素都包含在内。
score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
print(score[0:2])
print(score['cn':'math'])
输出2遍:
cn 99.0
math 120.0
dtype: float64
score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
print(len(score))
print(score.size)
print(score.shape)
输出:
3
3
(3,)
注意这里的shape返回多维数组的每一个维度。
在索引的方括号里还可以直接写条件表达式,将满足条件的数据直接取出来。
score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
print(score[score > 100])
输出:
math 120.0
en 114.0
dtype: float64
python里列表的加法是concat,而Series里的运算则是向量运算。
score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
score2 = score - 10.0
print(score2)
输出:
cn 89.0
math 110.0
en 104.0
dtype: float64
两个Series之间的运算则是根据标签来合并的,如果某个标签没有值,则显示NaN(缺失值not a number)。
score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
score2 = score + pd.Series({'cn':10.0, 'math':0, 'phy':70.0})
print(score2)
输出:
cn 109.0
en NaN
math 120.0
phy NaN
dtype: float64
下面的代码里,a没有重复元素,b里的元素1有重复。
a = pd.Series([1, 2, 3, 4, 5])
b = pd.Series([1, 1, 3, 4, 5])
print(a.is_unique)
print(b.is_unique)
输出:
True
False
下面的代码增加了物理成绩。
score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
score['phy'] = 70.0
print(score)
输出:
cn 99.0
math 120.0
en 114.0
phy 70.0
dtype: float64
也可以用切片修改多个元素。
score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
score['cn':'math'] = 0
print(score)
输出:
cn 0.0
math 0.0
en 114.0
dtype: float64
score1 = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
score2 = pd.Series({'phy':70, 'chem':66})
score3 = pd.concat([score1, score2])
print(score3)
输出:
cn 99.0
math 120.0
en 114.0
phy 70.0
chem 66.0
dtype: float64
用python里的del可以删除元素。另外一种办法是drop()函数。
要注意drop()的默认行为,是返回一个删除了元素之后的对象,原来的Series并没有任何改变。
score1 = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
score2 = score1.drop(index='cn')
print(score1)
print(score2)
输出:
cn 99.0
math 120.0
en 114.0
dtype: float64
math 120.0
en 114.0
dtype: float64
如果想在原来的Series上执行删除操作,则要指定inplace=True参数。
score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
score.drop(index=['cn','en'], inplace=True)
print(score)
输出:
math 120.0
dtype: float64
找出最大的2个,最小的2个。
ser = pd.Series([99, 120, 114, 70, 49.5, 130, 49, 48.5, 49.5, 60])
print(ser.nlargest(2))
print(ser.nsmallest(2))
输出:
5 130.0
1 120.0
dtype: float64
7 48.5
6 49.0
dtype: float64
ser = pd.Series(['a','a','a','b','b','c'])
print(ser.value_counts())
输出:
a 3
b 2
c 1
dtype: int64