python資料分析筆記---Pandas等基礎知識程式碼(1)
Pandas的資料結構 Seires
import
pandas
as
pd
import
numpy
as
np
s1
=
pd
。
Series
([
1
,
2
,
3
,
4
,
5
])
s1
type
(
s1
)
arr1
=
np
。
arange
(
1
,
6
)
arr1
#一個程式碼行只輸出最後一個 需要多列印每個加print
s2
=
pd
。
Series
(
arr1
)
s2
s2
=
pd
。
Series
(
arr1
,
index
=
[
‘a’
,
‘b’
,
‘c’
,
‘d’
,
‘e’
])
s2
s1
。
values
s1
。
index
#透過字典建立
dict
=
{
‘name’
:
‘李寧’
,
‘age’
:
18
,
‘class’
:
‘三班’
}
s3
=
pd
。
Series
(
dict
,
index
=
[
‘name’
,
‘age’
,
‘class’
,
‘sex’
])
s3
## Series 基本用法
s3
。
isnull
()
s3
。
notnull
()
(
s3
。
index
)
(
s3
。
values
)
s3
[
1
]
#標籤名
s3
[
‘age’
]
#選取多個
s3
[[
1
,
3
]]
s3
[[
‘name’
,
‘age’
]]
#切片
s3
[
1
:
3
]
s3
[
‘name’
:
‘class’
]
#標籤切片包含末端資料
s2
#布林索引
s2
[
s2
>
3
]
#索引與資料的對應關係不被運算結果影響
(
s2
*
2
)
(
s2
>
2
)
#name 屬性
s2
。
name
=
‘temp’
#物件名
s2
。
index
。
name
=
‘year’
#物件索引名
s2
s2
。
head
()
s2
。
tail
(
2
)
Pandas資料結構 DataFrame
import pandas as pd
import numpy as np
#構造一個字典
data={‘a’:[1,2,3,4],
‘b’:(5,6,7,8),
‘c’:np。arange(9,13)}
#構造dataframe
frame=pd。DataFrame(data)
frame
#index屬性檢視行索引
frame。index
#cloumns屬性檢視列索引
frame。columns
#valuse屬性檢視值
frame。values
#指定index
frame=pd。DataFrame(data,index=[‘A’,‘B’,‘C’,‘D’],columns=[‘a’,‘b’,‘c’,‘d’])
frame
#Series 構成的字典構造dataframe
pd1=pd。DataFrame({‘a’:pd。Series(np。arange(3)),
‘b’:pd。Series(np。arange(3,5))})
pd1
#字典構造成的字典構造dataframe
data1={
‘a’:{‘apple’:3。6,‘banana’:5。6},
‘b’:{‘apple’:3,‘banana’:5},
‘c’:{‘apple’:3。2}
}
pd2=pd。DataFrame(data1)
pd2
#構造二維陣列物件
arr1=np。arange(12)。reshape(4,3)
frame1=pd。DataFrame(arr1)
frame1
#字典構成的列表構造dataframe 注意和字典構成的字典的差異 行列不同
l1= [{‘apple’:3。6,‘banana’:5。6},{‘apple’:3,‘banana’:5},{‘apple’:3。2}]
pd3=pd。DataFrame(l1)
pd3
l2=[pd。Series(np。random。rand(3)),pd。Series(np。random。rand(2))]
pd4=pd。DataFrame(l2)
pd4
pd5=pd。DataFrame(np。arange(9)。reshape(3,3),index=[‘a’,‘c’,‘b’],columns=[‘A’,‘B’,‘C’])
pd5
pd5。T
pd5[‘A’]
print(type(pd5[‘A’]))
pd5[‘D’]=[1,2,3]
pd5
del(pd5[‘D’])
pd5
Pandas索引操作
import pandas as pd
import numpy as np
#Series 和DataFrame中的索引都是index物件
ps=pd。Series(range(5),index=[‘a’,‘b’,‘c’,‘d’,‘e’])
ps
pd1=pd。DataFrame(np。arange(9)。reshape(3,3),index=[‘a’,‘b’,‘c’],columns=[‘A’,‘B’,‘C’])
pd1
ps[0]=2
ps
#索引物件不可變 保證資料安全 此程式碼報錯
ps。index[0]=2
ps
## 常見的index種類
Index 索引
Int64Index 整數索引
Multilndex 層級索引
Datetimelndex 時間戳索引
#reindex 建立一個符合新索引的新物件
ps2=ps。reindex([‘a’,‘b’,‘c’,‘d’,‘e’,‘f’])
ps2
#結果f 顯示NAN
#行索引重建
pd2=pd1。reindex([‘a’,‘b’,‘c’,‘d’])
pd
#列索引重建
pd3=pd1。reindex(columns=[‘C’,‘B’,‘A’])
pd3
#增 操作
ps
ps[‘g’]=9
ps
s1=pd。Series({‘f’:999})
ps3=ps。append(s1)
ps3
pd1
pd1[4]=9
pd1
#增加列
pd1[4]=[10,11,12]
pd1
#插入
pd1。insert(0,‘E’,[9,99,999])
pd1
#增加行
pd1[][4]=[1,2,3,4,5]
pd1 #此程式碼語法錯誤
#標籤索引loc
pd1。loc[‘d’]=[1,1,1,1,1]
pd1
row={‘E’:6,‘A’:6,‘B’:6,‘C’:6,4:6}
pd5=pd1。append(row) #此程式碼語法錯誤 TypeError(‘Can only append a Series if ignore_index=True’
pd5
row={‘E’:6,‘A’:6,‘B’:6,‘C’:6,4:6}
pd5=pd1。append(row,ignore_index=True)
pd5
#del
ps
del ps[‘b’]
ps
pd1
del pd1[‘E’]
pd1
del pd1[‘d’]
pd1 #此程式碼錯誤 del只能刪除列
#drop 刪除軸上資料
#刪除一條
ps6=ps。drop(‘g’)
ps6 #產生一個新的變數 也就是說在ps上不刪除 但是在新的ps6上刪除了
#刪除多條
ps。drop([‘c’,‘d’])
#dataframe
pd1。drop(‘a’)
pd1。drop([‘a’,‘d’])
#刪除列
pd1。drop(‘A’,axis=1) #axis 1是刪除列 0是行 刪除是特殊情況
pd1。drop(‘A’,axis=‘columns’)
#inplace 屬性 在原物件上刪除 並不會返回新的物件
ps
ps。drop(‘g’,inplace=True)
ps
# 改 操作
ps1=pd。Series(range(5),index=[‘a’,‘b’,‘c’,‘d’,‘e’])
print(type(ps1。index))
ps1
pd1=pd。DataFrame(np。arange(9)。reshape(3,3),index=[‘a’,‘b’,‘c’],columns=[‘A’,‘B’,‘C’])
pd1
ps1[‘a’]=999
ps1
ps1[0]=888
ps1
#物件 列
pd1。A=6
pd1
#變成增加列操作
pd1[‘a’]=777
pd1
#loc 標籤索引
pd1。loc[‘a’]=777
pd1
pd1。loc[‘a’,‘A’]=1000
pd1
#查 操作
#Series
#1。行索引
ps1
ps1[‘a’] #標籤索引
ps1[0] #位置索引
#位置切片索引
ps1[1:4]
#標籤切片索引 按照索引名切片操作 包含最終索引
ps1[‘b’:‘e’]
#不連續索引
ps1[‘b’,‘e’] #語法錯誤
ps1[[‘b’,‘e’]]
ps1[[0,2,3]]
#布林索引
ps1>2
ps1[ps1>2]
#dataframe
pd1
#列索引
pd1[‘A’]
pd1[[0]] #報錯 位置索引不能這麼用
#取多列
pd1[[‘A’,‘C’]]
#選取一個值
pd1[‘A’][‘a’]
#切片
pd1[:2] #獲取前兩行
高階索引
loc 標籤索引
iloc 位置索引
ix標籤與位置混合索引
#loc 標籤索引
#loc 是基於標籤名的索引自定義索引名
ps1[‘a’:‘c’]
pd1
pd1。loc[0:2,‘A’] #第一個引數是索引行 第二個是列 這是錯的
pd1。loc[‘a’:‘b’,‘A’]
pd1。loc[‘a’:‘b’,‘A’:‘C’]
#iloc位置索引
ps1。iloc[1:3]
pd1。iloc[0:2,0:3]
#混合使用ix標籤與位置索引
ps1。ix[1:3]
pd1。ix[0:2,0] #只供瞭解 已經棄用了