Python機器學習三大件之二pandas
一、Pandas
2008年WesMcKinney開發(fā)出的庫
專門用于數(shù)據(jù)挖掘的開源python庫
以Numpy為基礎,借力Numpy模塊在計算方面性能高的優(yōu)勢
基于matplotlib,能夠簡便的畫圖
獨特的數(shù)據(jù)結構
二、數(shù)據(jù)結構
- Pandas中一共有三種數(shù)據(jù)結構,分別為:Series、DataFrame和MultiIndex。
三、Series
Series是一個類似于一維數(shù)組的數(shù)據(jù)結構,它能夠保存任何類型的數(shù)據(jù),比如整數(shù)、字符串、浮點數(shù)等,主要由一組數(shù)據(jù)和與之相關的索引兩部分構成。
- Series的創(chuàng)建
import pandas as pd pd.Series(np.arange(3))
0 0
1 1
2 2
dtype: int64
#指定索引 pd.Series([6.7,5.6,3,10,2], index=[1,2,3,4,5])
1 6.7
2 5.6
3 3.0
4 10.0
5 2.0
dtype: float64
#通過字典數(shù)據(jù)創(chuàng)建 color_count = pd.Series({'red':100, 'blue':200, 'green': 500, 'yellow':1000}) color_count
blue 200
green 500
red 100
yellow 1000
dtype: int64
- Series的屬性
color_count.index color_count.values
也可以使用索引來獲取數(shù)據(jù):
color_count[2]
100
- Series排序
data[‘p_change'].sort_values(ascending=True) # 對值進行排序
data[‘p_change'].sort_index() # 對索引進行排序
#series排序時,只有一列,不需要參數(shù)
四、DataFrame
創(chuàng)建
pd.DataFrame(np.random.randn(2,3))
score = np.random.randint(40, 100, (10, 5)) score
array([[92, 55, 78, 50, 50],
[71, 76, 50, 48, 96],
[45, 84, 78, 51, 68],
[81, 91, 56, 54, 76],
[86, 66, 77, 67, 95],
[46, 86, 56, 61, 99],
[46, 95, 44, 46, 56],
[80, 50, 45, 65, 57],
[41, 93, 90, 41, 97],
[65, 83, 57, 57, 40]])
但是這樣的數(shù)據(jù)形式很難看到存儲的是什么的樣的數(shù)據(jù),可讀性比較差??!
# 使用Pandas中的數(shù)據(jù)結構 score_df = pd.DataFrame(score)
- DataFrame的屬性
data.shape
data.index
data.columns
data.values
data.T
data.head(5)
data.tail(5)
data.reset_index(keys, drop=True)
keys : 列索引名成或者列索引名稱的列表
drop : boolean, default True.當做新的索引,刪除原來的列
- dataframe基本數(shù)據(jù)操作
data[‘open'][‘2018-02-27'] # 直接使用行列索引名字的方式(先列后行)
data.loc[‘2018-02-27':‘2018-02-22', ‘open'] # 使用loc:只能指定行列索引的名字
data.iloc[:3, :5 ]# 使用iloc可以通過索引的下標去獲取
data.sort_values(by=“open”, ascending=True) #單個排序
data.sort_values(by=[‘open', ‘high']) # 按照多個鍵進行排序
data.sort_index() # 對索引進行排序
DataFrame運算
應用add等實現(xiàn)數(shù)據(jù)間的加、減法運算
應用邏輯運算符號實現(xiàn)數(shù)據(jù)的邏輯篩選
應用isin, query實現(xiàn)數(shù)據(jù)的篩選
使用describe完成綜合統(tǒng)計
使用max, min, mean, std完成統(tǒng)計計算
使用idxmin、idxmax完成最大值最小值的索引
使用cumsum等實現(xiàn)累計分析
應用apply函數(shù)實現(xiàn)數(shù)據(jù)的自定義處理
五、pandas.DataFrame.plot
DataFrame.plot(kind=‘line')
kind : str,需要繪制圖形的種類
‘line' : line plot (default)
‘bar' : vertical bar plot
‘barh' : horizontal bar plot
關于“barh”的解釋:
http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.barh.html
‘hist' : histogram
‘pie' : pie plot
‘scatter' : scatter plot
六、缺失值處理
isnull、notnull判斷是否存在缺失值
np.any(pd.isnull(movie)) # 里面如果有一個缺失值,就返回True
np.all(pd.notnull(movie)) # 里面如果有一個缺失值,就返回False
dropna刪除np.nan標記的缺失值
movie.dropna()
fillna填充缺失值
movie[i].fillna(value=movie[i].mean(), inplace=True)
replace替換
wis.replace(to_replace="?", value=np.NaN)
七、數(shù)據(jù)離散化
p_change= data['p_change'] # 自行分組,每組個數(shù)差不多 qcut = pd.qcut(p_change, 10) # 計算分到每個組數(shù)據(jù)個數(shù) qcut.value_counts()
# 自己指定分組區(qū)間 bins = [-100, -7, -5, -3, 0, 3, 5, 7, 100] p_counts = pd.cut(p_change, bins)
得出one-hot編碼矩陣
dummies = pd.get_dummies(p_counts, prefix="rise") #prefix:分組名字前綴
八、數(shù)據(jù)合并
pd.concat([data1, data2], axis=1)
按照行或列進行合并,axis=0為列索引,axis=1為行索引pd.merge(left, right, how=‘inner', on=None)
可以指定按照兩組數(shù)據(jù)的共同鍵值對合并或者左右各自
left: DataFrame
right: 另一個DataFrame
on: 指定的共同鍵
how:按照什么方式連接
九、交叉表與透視表
交叉表:計算一列數(shù)據(jù)對于另外一列數(shù)據(jù)的分組個數(shù) 透視表:指定某一列對另一列的關系
#通過交叉表找尋兩列數(shù)據(jù)的關系 count = pd.crosstab(data['week'], data['posi_neg']) #通過透視表,將整個過程變成更簡單一些 data.pivot_table(['posi_neg'], index='week')
十、數(shù)據(jù)聚合
count = starbucks.groupby(['Country']).count() col.groupby(['color'])['price1'].mean() #拋開聚合談分組,無意義
到此這篇關于Python機器學習三大件之二pandas的文章就介紹到這了,更多相關Python pandas內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- 利用python Pandas實現(xiàn)批量拆分Excel與合并Excel
- 解決python3安裝pandas出錯的問題
- Python Pandas知識點之缺失值處理詳解
- Python基礎之pandas數(shù)據(jù)合并
- python基于Pandas讀寫MySQL數(shù)據(jù)庫
- python pandas合并Sheet,處理列亂序和出現(xiàn)Unnamed列的解決
- python 使用pandas同時對多列進行賦值
- Python3 pandas.concat的用法說明
- python pandas模糊匹配 讀取Excel后 獲取指定指標的操作
- Python數(shù)據(jù)分析之pandas讀取數(shù)據(jù)
相關文章
Spring Cloud Stream分區(qū)分組原理圖解
這篇文章主要介紹了Spring Cloud Stream的分區(qū)和分組,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03Java下SpringBoot創(chuàng)建定時任務詳解
這篇文章主要介紹了Java下SpringBoot創(chuàng)建定時任務詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07將RestTemplate的編碼格式改為UTF-8,防止亂碼問題
這篇文章主要介紹了將RestTemplate的編碼格式改為UTF-8,防止亂碼問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10