1 Pandas序列Series
1.1 根據串列生成序列 Series
X=[1,3,6,4,9];X
weight=[67,66,83,68,70];weight
sex=['女','男','男','女','男'];sex
S1=pd.Series(X);S1
S2=pd.Series(weight);S2
S3=pd.Series(sex);S3

1.2 序列合并concat
pd.concat([S2,S3],axis=0) # 0維,附加到序列后面
pd.concat([S2,S3],axis=1) # 1維,附加到新的一列

1.3 序列切片
S1[2]
S3[1:4]

2 Pandas資料框DataFrame
2.1 空資料框DataFrame
pd.DataFrame()
2.2 根據串列創建資料框columns index
pd.DataFrame(X)
# 創建一個列名為`weight`,索引為`A` `B` `C` `D` `E`的資料框
pd.DataFrame(weight,columns=['weight'],index=['A','B','C','D','E'])

2.3 根據字典創建資料框
df1=pd.DataFrame({'S1':S1,'S2':S2,'S3':S3});df1

# 索引來自串列`X`
df2=pd.DataFrame({'sex':sex,'weight':weight},index=X);df2

2.4 增加資料框列
df2['weight2']=df2['weight']**2;df2

2.5 洗掉資料框列del
del df2['weight2'];df2

2.6 缺失值處理 isnull() isnull().sum() dropna()
df3=pd.DataFrame({'S2':S2,'S3':S3},index=S1);df3
df3.isnull() # 缺失值則回傳 True,否則回傳 False

df3.isnull().sum() # 回傳每列中包含的缺失值個數

df3.dropna() # 直接洗掉含有缺失值的行,多變數謹慎使用 不改變df3

2.7 資料框排序
df3.sort_index() # 按index排序
df3.sort_values(by='S3') # 按S3列值排序

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/509629.html
標籤:其他
下一篇:pandas (一)
