Series
Pandas各個(gè)數(shù)據(jù)類型的關(guān)系:0維單值變量->1維Series->2維DataFrame->3維層次化DataFrame
wd.png
import numpy as np
import pandas as pd
創(chuàng)建
- 列表創(chuàng)建
- 字典創(chuàng)建
- 其他創(chuàng)建
列表創(chuàng)建Series
a = pd.Series([2,3,4,5,6])
a
0 2
1 3
2 4
3 5
4 6
dtype: int64
# 自定義索引
b = pd.Series([2,3,4,5,6], index=['a', 'b', 'c', 'd', 'e'])
b
a 2
b 3
c 4
d 5
e 6
dtype: int64
Series可以存儲(chǔ)不同類型數(shù)據(jù)
雖然可以,但建議存儲(chǔ)同一類型數(shù)據(jù)臊泰,因?yàn)镾eries更像表格中的列愕秫,列應(yīng)該是同一類型的
c = pd.Series([18, 28, 38])
c
0 18
1 28
2 38
dtype: int64
c2 = pd.Series(['張三', 18, 85.5, True])
c2
0 張三
1 18
2 85.5
3 True
dtype: object
字典創(chuàng)建Series
# 索引就是字典的鍵
d = pd.Series({
'name': '張三',
'age': 18,
'gander': True,
})
d
name 張三
age 18
gander True
dtype: object
# 創(chuàng)建時(shí)自定義索引會(huì)替換字典索引
d2 = {
'name': '張三',
'age': 18,
'gander': True,
}
d = pd.Series(d2, index=['name', 'age', 'score'])
d
name 張三
age 18
score NaN
dtype: object
其他方式
# 標(biāo)量創(chuàng)建
pd.Series(5)
pd.Series(5, index=[1,2,3,4,5])
1 5
2 5
3 5
4 5
5 5
dtype: int64
# 序列
range(5)
list(range(5))
for i in range(5):
print(i)
pd.Series(range(5))
0
1
2
3
4
0 0
1 1
2 2
3 3
4 4
dtype: int64
# Numpy的序列函數(shù)創(chuàng)建
np.arange(5)
np.arange(2, 5)
np.arange(9, 5, -1)
array([9, 8, 7, 6])
pd.Series(
np.arange(4),
index=np.arange(9, 5, -1)
)
9 0
8 1
7 2
6 3
dtype: int32
查詢
class1 = pd.Series([95, 25, 59, 90, 61], index=['ming', 'hua', 'hong', 'huang', 'bai'])
class1
ming 95
hua 25
hong 59
huang 90
bai 61
dtype: int64
查詢數(shù)據(jù)形狀
1維數(shù)據(jù)的形狀就是它的值個(gè)數(shù)
class1.shape
class1.shape[0]
5
查詢值(values)和索引(index)
一個(gè)Series數(shù)據(jù)是由2個(gè)ndarray數(shù)組組成的
# 查詢值
class1.values
array([95, 25, 59, 90, 61], dtype=int64)
type(class1.values)
numpy.ndarray
# 查詢索引
class1.index
Index(['ming', 'hua', 'hong', 'huang', 'bai'], dtype='object')
class1.index.values # 索引的本質(zhì)也是數(shù)組
array(['ming', 'hua', 'hong', 'huang', 'bai'], dtype=object)
# 查詢單個(gè)索引或值
class1.values[2], class1.index[2], class1.index.values[2]
(59, 'hong', 'hong')
查詢值
- 根據(jù)索引查詢值
- 索引查詢
- 切片查詢
- 根據(jù)條件反查索引
- 布爾查詢
索引查詢
索引和切片查詢都是根據(jù)索引查詢值
class1
ming 95
hua 25
hong 59
huang 90
bai 61
dtype: int64
查詢單值
# Series有兩套索引:默認(rèn)索引适篙,自定義索引
class1['hong'] # 自定義索引
class1[2] # 默認(rèn)索引
59
查詢多值
class1[['hua', 'huang']]
hua 25
huang 90
dtype: int64
class1[[1, 3]]
hua 25
huang 90
dtype: int64
# class1[[1, 'hong']] # 索引不能混用
切片查詢
class1
ming 95
hua 25
hong 59
huang 90
bai 61
dtype: int64
# 默認(rèn)索引:包含起始值,不包含結(jié)束值
class1[:3]
class1[2:]
class1[1:4]
hua 25
hong 59
huang 90
dtype: int64
# 自定義索引:包含起始和結(jié)束值
# 原因是自定義索引沒有順序赁酝,難以確定索引前后的值
class1['hua':'huang']
hua 25
hong 59
huang 90
dtype: int64
# 步長
class1[::2]
ming 95
hong 59
bai 61
dtype: int64
# 倒查
class1[::-1]
bai 61
huang 90
hong 59
hua 25
ming 95
dtype: int64
布爾查詢
根據(jù)值反查索引
根據(jù)條件反查索引
class1
ming 95
hua 25
hong 59
huang 90
bai 61
dtype: int64
# 原生方式查詢
for i in class1:
# print(i)
if i < 60:
print(i)
25
59
# 布爾查詢
class1[[True, True, False, False, False]]
ming 95
hua 25
dtype: int64
class1 < 60
ming False
hua True
hong True
huang False
bai False
dtype: bool
# 布爾查詢,布爾值由程序判斷自動(dòng)生成
class1[class1 < 60]
hua 25
hong 59
dtype: int64
向量化運(yùn)算
矢量運(yùn)算,并行運(yùn)算
所有同學(xué)加5分
x = {
'ming': 95,
'hua': 25,
'hong': 59,
'huang': 90,
'bai': 61,
}
x
{'ming': 95, 'hua': 25, 'hong': 59, 'huang': 90, 'bai': 61}
原生Python字典運(yùn)算
# 原生python智亮,遍歷序列,運(yùn)算
# 速度慢点待,效率低
for i in x:
print(i, x[i]+5)
ming 100
hua 30
hong 64
huang 95
bai 66
Pandas向量化運(yùn)算
class1 = pd.Series([95, 25, 59, 90, 61], index=['ming', 'hua', 'hong', 'huang', 'bai'])
class1
ming 95
hua 25
hong 59
huang 90
bai 61
dtype: int64
# 向量化運(yùn)算阔蛉,不需要遍歷,速度快效率高
class1 + 5
ming 100
hua 30
hong 64
huang 95
bai 66
dtype: int64
應(yīng)用函數(shù)執(zhí)行向量化運(yùn)算
x
{'ming': 95, 'hua': 25, 'hong': 59, 'huang': 90, 'bai': 61}
class1
ming 95
hua 25
hong 59
huang 90
bai 61
dtype: int64
計(jì)算總分
# Python原生方式
y = 0
for i in x:
y = y + x[i]
y
330
# Pandas向量化方式
class1.sum() # Pandas方法
np.sum(class1) # Numpy方法
330
求平均分
# Python原生方式
y = 0
for i in x:
y = y + x[i]
y/len(x)
66.0
# Pandas向量化方式
class1.sum() / class1.shape[0] # shape[0]就是讀取矩陣第一維度的長度
class1.mean()
np.mean(class1)
66.0
類Numpy數(shù)組操作癞埠,和類Python字典的操作
- Pandas數(shù)據(jù)可以執(zhí)行全部Numpy數(shù)據(jù)操作(因?yàn)镻andas底層基于Numpy状原,所以通用)
- 也可以執(zhí)行部分Python原生列表或字典操作(僅限于Pandas實(shí)現(xiàn)的操作)
- 保留字in操作
- 使用.get()方法
x
{'ming': 95, 'hua': 25, 'hong': 59, 'huang': 90, 'bai': 61}
class1
ming 95
hua 25
hong 59
huang 90
bai 61
dtype: int64
類Numpy數(shù)組操作
np.mean(class1) # Numpy方法
66.0
類Python字典操作
- in關(guān)鍵字:判斷某索引是否存在
- get方法:判斷某索引是否存在,存在則直接輸出值苗踪,不存在則輸出定義值
# 字典方法
'ming' in x
'hei' in x
False
# Pandas方法
'ming' in class1
'hei' in class1
False
# 字典方法
x.get('ming', 60)
x.get('hei', 60)
60
# Pandas方法
class1.get('ming', 60)
class1.get('hei', 60)
60
修改
class1
ming 95
hua 25
hong 59
huang 90
bai 61
dtype: int64
修改 值颠区,values
class1['hua'] # 查詢
25
class1['hua'] = 35
class1
ming 95
hua 35
hong 59
huang 90
bai 61
dtype: int64
# 修改多值
class1[['hua', 'hong']]
class1[['hua', 'hong']] = [32, 59.5]
class1['hua', 'hong'] = [33, 59.6] # 賦值使用單層括號
class1
ming 95.0
hua 33.0
hong 59.6
huang 90.0
bai 61.0
dtype: float64
修改索引,index
class1.index # 索引
class1.index[0] # 索引單值
# class1.index[0] = 'xiaoming' # 報(bào)錯(cuò)通铲,不能直接修改索引
'ming'
class1.index.values # series的索引底層是數(shù)組
array(['ming', 'hua', 'hong', 'huang', 'bai'], dtype=object)
class1.index.values[0]
class1.index.values[0] = 'xiaoming' # 錯(cuò)誤操作瓦呼,直接修改底層索引,見后
class1
xiaoming 95.0
hua 33.0
hong 59.6
huang 90.0
bai 61.0
dtype: float64
class1['hua']
class1['ming']
# class1['xiaoming'] # 報(bào)錯(cuò),直接修改底層索引后無法查詢
33.0
正確操作:使用rename方法修改單個(gè)索引
class11 = class1.rename({'xiaoming': '小明', 'hong': '小紅'})
class11
小明 95.0
hua 33.0
小紅 59.6
huang 90.0
bai 61.0
dtype: float64
class11['小明']
95.0
class1 # rename沒有修改原值
xiaoming 95.0
hua 33.0
hong 59.6
huang 90.0
bai 61.0
dtype: float64
Series的層次化索引(了解)
層次化索引會(huì)增加Series的維度
class1
xiaoming 95.0
hua 33.0
hong 59.6
huang 90.0
bai 61.0
dtype: float64
class2 = pd.Series([95, 25, 59, 90, 61], index=[['ming', 'ming', 'hong', 'huang', 'bai'], [2,4,6,8,10]])
class2
ming 2 95
4 25
hong 6 59
huang 8 90
bai 10 61
dtype: int64
層次化索引查詢
# 先查最外層索引
class2['ming']
2 95
4 25
dtype: int64
type(class2['ming'])
pandas.core.series.Series
# 再往里層索引查詢
class2['ming'][2]
class2['ming', 2] # 推薦央串,逗號分隔磨澡,每層都是一個(gè)維度
95
type(class2['ming', 2])
numpy.int64
將層次化索引的Series轉(zhuǎn)為DataFrame
unstack()方法將Series內(nèi)層索引旋轉(zhuǎn)為DataFrame的列索引
行轉(zhuǎn)列,將數(shù)據(jù)的行“旋轉(zhuǎn)”為列
class2
ming 2 95
4 25
hong 6 59
huang 8 90
bai 10 61
dtype: int64
class2.unstack()
image.png