- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- Pandas的介紹及安裝方法
這篇文章主要介紹“Pandas的介紹及安裝方法”,在日常操作中,相信很多人在Pandas的介紹及安裝方法問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對大家解答”P(pán)andas的介紹及安裝方法”的疑惑有所幫助!接下來(lái),請跟著(zhù)小編一起來(lái)學(xué)習吧!
Pandas介紹與安裝
為什么會(huì )有Pandas
Pandas支持大部分Numpy語(yǔ)言風(fēng)格,尤其是數組函數與廣播機制的各種數據處理。單是Numpy更適合處理同質(zhì)型的數據。而Pandas的設計就是用來(lái)處理表格型或異性數據的,高效的清洗、處理數據。
Pandas是什么?
Pandas是基于Numpy的一種工具,提供高性能矩陣的運算,該工具是為了解決數據分析任何而創(chuàng )建的。也是貫穿整個(gè)Python數據分析非常核心的工具
安裝Pandas
pip install Pandas
Pandas涉及內容
Pandas基礎 、數據清洗與準備、數據聚合與分組、時(shí)間序列
Pandas數據結構介紹
Series介紹
Series是一種一維數組對象,它包含了一個(gè)值序列(value) ,并且包含了數據標簽,稱(chēng)之為索引 (index)
Series創(chuàng )建
pd.Series(data=None,index=None,dtype=None,name=None,copy=False)
data : 創(chuàng )建數組的數據,可為array、like、dict、or scalar value
index : 指定索引
dtype : 數組數據類(lèi)型
name : 數組名稱(chēng)
copy : 是否拷貝
Pandas數組函數
語(yǔ)法 基本使用
dtype 查看數據類(lèi)型
astype 修改數據類(lèi)型
head() 預覽前幾條數據
tail() 預覽后幾條數據
In [15]: # 指定索引序列
In [16]: series = pd.Series(np.arange(4),index=['a','b','c','d'])
In [17]: series
Out[17]:
a 0
b 1
c 2
d 3
dtype: int32
In [18]: # 指定索引的名字
In [19]: series = pd.Series(np.arange(4),index=['a','b','c','d'],name='SmallJ')
In [20]: series
Out[20]:
a 0
b 1
c 2
d 3
Name: SmallJ, dtype: int32
In [21]: # 默認返回int32,可指定其他類(lèi)型
In [23]: series = pd.Series(np.arange(4),index=['a','b','c','d'],name='SmallJ',dtype='int64')
In [24]: series
Out[24]:
a 0
b 1
c 2
d 3
Name: SmallJ, dtype: int64
In [29]: import numpy as np
In [30]: import pandas as pd
In [31]: series = pd.Series(np.arange(10),name='SmallJ')
In [32]: series
Out[32]:
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
Name: SmallJ, dtype: int32
In [33]: # 前面為索引后面為值
In [34]: series.dtype
Out[34]: dtype('int32')
In [35]: # 查看數據類(lèi)型
In [36]: series.dtype
Out[36]: dtype('int32')
In [37]: # 修改數據類(lèi)型
In [38]: series.astype('float64')
Out[38]:
0 0.0
1 1.0
2 2.0
3 3.0
4 4.0
5 5.0
6 6.0
7 7.0
8 8.0
9 9.0
Name: SmallJ, dtype: float64
In [39]: # 預覽從頭開(kāi)始的數據 (括號內填指定的數據)
In [40]: series.head(5)
Out[40]:
0 0
1 1
2 2
3 3
4 4
Name: SmallJ, dtype: int32
In [41]: series.head(6)
Out[41]:
0 0
1 1
2 2
3 3
4 4
5 5
Name: SmallJ, dtype: int32
In [42]: # 預覽最后的數據 (括號填指定的數據)
In [43]: series.tail(5)
Out[43]:
5 5
6 6
7 7
8 8
9 9
Name: SmallJ, dtype: int32
Series的索引與值
series.index
查看索引
series.values
查看值序列
series.reset_index(drop=False)
重置索引
drop 是否刪除原索引 默認為否
In [89]: import pandas as pd
In [90]: import numpy as np
In [91]: series = pd.Series(data=np.arange(5),index=['a','b','c','d','e'])
In [92]: series
Out[92]:
a 0
b 1
c 2
d 3
e 4
dtype: int32
In [93]: # 查看索引
In [94]: series.index
Out[94]: Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
In [95]: series.values
Out[95]: array([0, 1, 2, 3, 4])
In [96]: series.reset_index()
Out[96]:
index 0
0 a 0
1 b 1
2 c 2
3 d 3
4 e 4
In [98]: series
Out[98]:
a 0
b 1
c 2
d 3
e 4
dtype: int32
In [99]: # 查看值序列
In [100]: series.values
Out[100]: array([0, 1, 2, 3, 4])
In [101]: # 當drop中的值為T(mén)rue的時(shí)候將采用刪除原索引,并不會(huì )對原數據進(jìn)行修改,需要復制
In [102]: series = series.reset_index(drop=True)
In [103]: series
Out[103]:
0 0
1 1
2 2
3 3
4 4
dtype: int32
Series索引與切片
series.[‘標簽索引’]
通過(guò)標簽索引來(lái)取值
series[‘索引’]
通過(guò)下標索引來(lái)取值
series.loc(標簽索引)
通過(guò)標簽索引來(lái)取值
series.iloc(索引)
通過(guò)索引
In [115]: # 通過(guò)標簽來(lái)取值
In [116]: series.loc['b']
Out[116]: 1
In [117]: # 通過(guò)索引下標來(lái)取值
In [118]: series.iloc[1]
Out[118]: 1
采用神奇索引
In [139]: series
Out[139]:
a 0
b 1
c 10
d 3
e 22
dtype: int32
In [141]: # 采用標簽來(lái)取值
In [142]: series[['a','e']]
Out[142]:
a 0
e 22
dtype: int32
In [143]: # 采用索引取值
In [144]: series[[0,-1]]
Out[144]:
a 0
e 22
dtype: int32
Series修改值
In [122]: series
Out[122]:
a 0
b 1
c 2
d 3
e 4
dtype: int32
通過(guò)索引來(lái)修改值
series.iloc[2] = 10
通過(guò)標簽來(lái)修改值
series.loc['e'] = 22
In [139]: series
Out[139]:
a 0
b 1
c 10
d 3
e 22
dtype: int32
判斷值是否存在
in 并不是判斷值,而是根據標簽索引來(lái)判斷
Series運算
共同索引對應運算,其他值填充為NaN
Pandas會(huì )自動(dòng)幫我們進(jìn)行數據轉換,當我們的數據類(lèi)型為None時(shí),會(huì )把數據替換為NaN
當沒(méi)用通過(guò)索引的時(shí)候,將全部變?yōu)镹aN
NaN與任何值計算都是NaN
In [148]: data = pd.Series(data=[1,2,3,4,None],index=['a','b','c','d','e'])
In [149]: data
Out[149]:
a 1.0
b 2.0
c 3.0
d 4.0
e NaN
dtype: float64
當進(jìn)行對應標簽索引進(jìn)行相加的時(shí)候
In [148]: data = pd.Series(data=[1,2,3,4,None],index=['a','b','c','d','e'])
In [149]: data
Out[149]:
a 1.0
b 2.0
c 3.0
d 4.0
e NaN
dtype: float64
In [150]: data1 = pd.Series(data=[1,2,3,4,None],index=['a','b','c','d','e'])
In [151]: data1
Out[151]:
a 1.0
b 2.0
c 3.0
d 4.0
e NaN
dtype: float64
In [152]: data + data1
Out[152]:
a 2.0
b 4.0
c 6.0
d 8.0
e NaN
dtype: float64
當對應的標簽索引位置進(jìn)行相加時(shí)
當對應是索引的位置沒(méi)有數值時(shí),顯示的數值為NaN
In [148]: data = pd.Series(data=[1,2,3,4,None],index=['a','b','c','d','e'])
In [153]: data2 = pd.Series(data=[1,2,3],index=['a','b','c'])
In [156]: data
Out[156]:
a 1.0
b 2.0
c 3.0
d 4.0
e NaN
dtype: float64
In [157]: data2
Out[157]:
a 1
b 2
c 3
dtype: int64
In [158]: data + data2
Out[158]:
a 2.0
b 4.0
c 6.0
d NaN
e NaN
dtype: float64
當不對應的索引標簽進(jìn)行相加的時(shí)候
當對應的索引標簽不相同的時(shí),顯示的全部結果為NaN
In [161]: data2 = pd.Series(data=[1,2,3],index=['a','b','c'])
In [162]: data3 = pd.Series(data=[1,2,3,4],index=['d','e','f','g'])
In [163]: data2
Out[163]:
a 1
b 2
c 3
dtype: int64
In [164]: data3
Out[164]:
d 1
e 2
f 3
g 4
dtype: int64
In [165]: data2 + data3
Out[165]:
a NaN
b NaN
c NaN
d NaN
e NaN
f NaN
g NaN
dtype: float64
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站