python数据分析第三方库是_python数据分析复盘——数据分析相关库之Pandas
編輯推薦:
本文來源csdn,本文主要對Python的第三方庫Pandas,進行高性能易用數據類型和分析。
1.Pandas 簡介
1.1 pandas是什么
Pandas是Python第三方庫,提供高性能易用數據類型和分析工具
Pandas基于NumPy實現 ,常與NumPy和Matplotlib一同使用
1.2 pandas vs numpy
2.Pandas庫的Series類型
2.1 Series的結構
#多維一列,形式是:索引+值。(省略index會自動生成,從0開始)
>>> pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
a 1
b 2
c 3
d 4
e 5
dtype: int64
2.2 Series的創建
Series類型可以由如下類型創建:
1.Python列表
2.標量值
3.Python字典
4.ndarray
5.其他函數,range()等
#標量值
>>> pd.Series(5)
0 5
dtype: int64
#標量值+index 結果會根據索引重新排序
pd.Series(5,index=['a','v','c','d','e'])
a 5
v 5
c 5
d 5
e 5
dtype: int64
#字典
>>> pd.Series({'a':999,'v':888,'c':756,
'd':7,'e':437})
a 999
c 756
d 7
e 437
v 888
dtype: int64
#字典+index
>>>pd.Series({'a':999,'v':888,'c':756,
'd':7,'e':437},index=['a','v'])
a 999
v 888
dtype: int64
#用ndarray創建
>>> pd.Series(np.arange(5),index=np.
arange(14,9,-1))
14 0
13 1
12 2
11 3
10 4
dtype: int32
Seriesd的創建總結:
1.Series類型可以由如下類型創建:
2.Python列表,index與列表元素個數一致
3.標量值,index表達Series類型的尺寸
4.Python字典,鍵值對中的“鍵”是索引,index從字典中進行選擇操作
5.ndarray,索引和數據都可以通過ndarray類型創建
6.其他函數,range()函數等
2.3 Series基本操作
1.Series類型包括index和values兩部分
2.Series類型的操作類似ndarray類型
3.Series類型的操作類似Python字典類型
(1)Series基本操作
#Series基本操作
>>>a=pd.Series({'a':1,'v':2,'c':3,'d':4,'e':5})
>>> a.index
Index(['a', 'c', 'd', 'e', 'v'], dtype='object')
>>> a.values
array([1, 3, 4, 5, 2], dtype=int64)
#兩套索引并存,但不能混用
>>> a[['a','v']]
a 1
v 2
dtype: int64
>>> a[[0,4]]
a 1
v 2
dtype: int64
#混用,以靠前的為準
>>> a[['a',4]]
a 1.0
4 NaN
dtype: float64
(2)Series類型的操作類似ndarray類型:
索引方法相同,采用 [ ]
可以通過自定義索引的列表進行切片
可以通過自動索引進行切片,如果存在自定義索引,則一同被切片
#采用
[]切片
>>> a=pd.Series({'a':1,'v':2,'c':3,'d':4,'e':5})
>>> a[:3]
a 1
c 3
d 4
dtype: int64
#在索引前進行運算
>>> a[a>a.median()]
d 4
e 5
dtype: int64
#以自然常數e為底的指數函數
>>> np.exp(a)
a 2.718282
c 20.085537
d 54.598150
e 148.413159
v 7.389056
dtype: float64
(3)Series類型的操作(類似Python):
通過自定義索引訪問
保留字in操作
使用.get()方法
#保留字in
>>> a=pd.Series({'a':1,'v':2,'c':3,'d':4,'e':5})
>>> 'a' in a
True
>>> 'v' in a
True
#只匹配索引
>>> 1 in a
False
2.4 Series對齊操作
#Series類型在運算中會自動對齊不同索引的數據.(即對不齊,就當缺失項處理)
>>> a=pd.Series({'a':1,'v':2,'c':3,'d':4,'e':5})
>>> b=pd.Series({'a':1,'b':2,'c':3,'d':4,'e':5})
>>> a+b
a 2.0
b NaN
c 6.0
d 8.0
e 10.0
v NaN
dtype: float64
2.5 Series的name屬性
#Series對象和索引都可以有一個名字,存儲在屬性.name中
a=pd.Series({'a':1,'v':2,'c':3,'d':4,'e':5})
>>> a.name
>>> a.name="精忠跳水隊"
>>> a.name
'精忠跳水隊'
>>> a
a 1
c 3
d 4
e 5
v 2
Name: 精忠跳水隊, dtype: int64
2.6 Series小結
Series是一維帶“標簽”數組
index_0 → data_a
Series基本操作類似ndarray和字典,根據索引對齊
3.Pandas庫的DataFrame類型
3.1 DataFrame結構
#DataFrame是一個表格型的數據類型,每列值類型可以不同
#DataFrame既有行索引、也有列索引
>>> df = pd.DataFrame(np.random.randint(1,10,(4,5)))
>>> df
0 1 2 3 4
0 8 5 4 1 1
1 3 4 2 7 3
2 4 3 8 9 9
3 7 8 9 1 7
3.2 DataFrame的創建
DataFrame類型可以由如下類型創建:
ndarray對象
由一維ndarray、列表、字典、元組或Series構成的字典
Series類型
其他的DataFrame類型
#由字典創建
(自定義行列索引,會自動補齊缺失的值為NAN)
>>> df=pd.DataFrame({'one':pd.Series([1,2,3],index=
['a','v','c']),'two':pd.Series([1,2,3,4,5],index=
['a','b','c','d','e'])})
>>> df
one two
a 1.0 1.0
b NaN 2.0
c 3.0 3.0
d NaN 4.0
e NaN 5.0
v 2.0 NaN
#由字典+列表創建。統一index,尺寸必須相同
>>> df=pd.DataFrame({'one':[1,2,3],'two':[2,2,3],'three'
:[3,2,3]},index=['a','b','c'])
>>> df
one three two
a 1 3 2
b 2 2 2
c 3 3 3
#索引(類似Series,依據行列索引)
>>> df['one']['a']
1
>>> df['three']['c']
3
3.3 pandas數據類型操作——重新索引
#由a
b c改為c a b
>>> df.reindex(['c','a','b'])
one three two
c 3 3 3
a 1 3 2
b 2 2 2
#重排并增加列
>>> df.reindex(columns=['three','two','one','two'])
three two one two
a 3 2 1 2
b 2 2 2 2
c 3 3 3 3
#原始的數據
>>> df
one three two
a 1 3 2
b 2 2 2
c 3 3 3
#插入列
>>> newc=df.columns.insert(3,'新增')
>>> newc
Index(['one', 'three', 'two', '新增'], dtype='object')
#插入新數據
>>> newd=df.reindex(columns=newc,fill_value=99)
>>> newd
one three two 新增
a 1 3 2 99
b 2 2 2 99
c 3 3 3 99
3.4pandas數據類型操作——索引類型
>>>
df
one three two
a 1 3 2
b 2 2 2
c 3 3 3
>>> nc=df.columns.delete(1)
>>> ni=df.index.insert(3,'new_index')
#無填充
>>> df.reindex(columns=nc,index=ni)
one two
a 1.0 2.0
b 2.0 2.0
c 3.0 3.0
new_index NaN NaN
#有填充
>>> df.reindex(columns=nc,index=ni,method='ffill')
one two
a 1 2
b 2 2
c 3 3
new_index 3 3
#刪除行列
#默認刪 除行
>>> df.drop('b')
one three two
a 1 3 2
c 3 3 3
#軸1為列
>>> df.drop('three',axis=1)
one two
a 1 2
b 2 2
c 3 3
3.5pandas數據類型運算——算數運算
算數運算法則:
算術運算根據行列索引,補齊后運算,運算默認產生浮點數
補齊時缺項填充NaN (空值)
二維和一維、一維和零維間為廣播運算
采用+ ‐ * /符號進行的二元運算產生新的對象
(1)采用+ ‐ * /符號進行的二元運算:
#用符號運算,無法處理缺失值
>>> df1 =pd.DataFrame({'one':[1,2,3],'two':
[4,5,6]},index=['a','b','c'])
>>> df2 =pd.DataFrame({'one':[1,2,3]},
index=['a','b','c'])
>>> df1+df2
one two
a 2 NaN
b 4 NaN
c 6 NaN
(2)采用方法形式進行二元運算:
>>>
df1 =pd.DataFrame({'one':[1,2,3],'two':[4,5,6]},
index=['a','b','c'])
>>> df2 =pd.DataFrame({'one':[1,2,3]},index=['a','b','c'])
#用方法進行運算,可選參數處理缺失值
>>> df1.add(df2,fill_value=0)
one two
a 2 4.0
b 4 5.0
c 6 6.0
#運算方式
#只對對應維度及對應位置進行運算,常數則進行廣播運算。
無匹配位置,則置為NAN
df =pd.DataFrame({'one':[1,2,3],'two':[2,2,3],'three'
:[3,2,3]},index=['a','b','c'])
df3=df=pd.DataFrame({'one':[1,2,3]},index=['a','b','c'])
df4=pd.DataFrame({'two':[2,2,3]},index=['a','b','c'])
#常數
>>> df3-1
one
a 0
b 1
c 2
>>> df -1
one three two
a 0 2 1
b 1 1 1
c 2 2 2
#對應維度
>>> df3 -df
one three two
a 0 NaN NaN
b 0 NaN NaN
c 0 NaN NaN
>>> df-df4
one three two
a NaN NaN 0
b NaN NaN 0
c NaN NaN 0
3.6pandas數據類型運算——比較運算
(1)法則
比較運算只能比較相同索引的元素,不進行補齊
二維和一維、一維和零維間為廣播運算
采用>、<、 >=、 <= 、==、 !=等符號進行的二元運算產生布爾對象
>>>
dfx
one three two
a 1 3 2
b 1 3 2
c 1 3 2
>>> df
one three two
a 1 3 2
b 2 2 2
c 3 3 3
>>> df>dfx
one three two
a False False False
b True False False
c True False True
4.Pandas數據類型小結
1.據類型與索引的關系,操作索引即操作數據
2.Series = 索引+ 一維數據
3.DataFrame = 行列索引+ 多維數據
4.重新索引、數據刪除、算術運算、比較運算
5.像對待單一數據一樣對待Series和DataFrame對象
總結
以上是生活随笔為你收集整理的python数据分析第三方库是_python数据分析复盘——数据分析相关库之Pandas的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python array函数_Pytho
- 下一篇: python3写一个计算器_Python