相關(guān)學(xué)習(xí)推薦:python教程
Python 是開源的,它很棒,但是也無法避免開源的一些固有問題:很多包都在做(或者在嘗試做)同樣的事情。如果你是 Python 新手,那么你很難知道某個特定任務(wù)的最佳包是哪個,你需要有經(jīng)驗的人告訴你。有一個用于數(shù)據(jù)科學(xué)的包絕對是必需的,它就是 pandas。
pandas 最有趣的地方在于里面隱藏了很多包。它是一個核心包,里面有很多其他包的功能。這點(diǎn)很棒,因為你只需要使用 pandas 就可以完成工作。
pandas 相當(dāng)于 python 中 excel:它使用表(也就是 dataframe),能在數(shù)據(jù)上做各種變換,但還有其他很多功能。
如果你早已熟知 python 的使用,可以直接跳到第三段。
讓我們開始吧:
import pandas as pd復(fù)制代碼
別問為什么是「pd」而不是「p」,就是這樣。用就行了:)
pandas 最基本的功能
讀取數(shù)據(jù)
data = pd.read_csv( my_file.csv ) data = pd.read_csv( my_file.csv , sep= ; , encoding= latin-1 , nrows=1000, skiprows=[2,5])復(fù)制代碼
sep 代表的是分隔符。如果你在使用法語數(shù)據(jù),excel 中 csv 分隔符是「;」,因此你需要顯式地指定它。編碼設(shè)置為 latin-1 來讀取法語字符。nrows=1000 表示讀取前 1000 行數(shù)據(jù)。skiprows=[2,5] 表示你在讀取文件的時候會移除第 2 行和第 5 行。
-
最常用的功能:read_csv, read_excel
-
其他一些很棒的功能:read_clipboard, read_sql
寫數(shù)據(jù)
data.to_csv( my_new_file.csv , index=None)復(fù)制代碼
index=None 表示將會以數(shù)據(jù)本來的樣子寫入。如果沒有寫 index=None,你會多出一個第一列,內(nèi)容是 1,2,3,…,一直到最后一行。
我通常不會去使用其他的函數(shù),像.to_excel, .to_json, .to_pickle 等等,因為.to_csv 就能很好地完成工作,并且 csv 是最常用的表格保存方式。
檢查數(shù)據(jù)

Gives (#rows, #columns)復(fù)制代碼
給出行數(shù)和列數(shù)
data.describe()復(fù)制代碼
計算基本的統(tǒng)計數(shù)據(jù)
查看數(shù)據(jù)
data.head(3)復(fù)制代碼
打印出數(shù)據(jù)的前 3 行。與之類似,.tail() 對應(yīng)的是數(shù)據(jù)的最后一行。
data.loc[8]復(fù)制代碼
打印出第八行
data.loc[8, column_1 ]復(fù)制代碼
打印第八行名為「column_1」的列
data.loc[range(4,6)]復(fù)制代碼
第四到第六行(左閉右開)的數(shù)據(jù)子集
pandas 的基本函數(shù)
邏輯運(yùn)算
data[data[ column_1 ]== french ] data[(data[ column_1 ]== french ) & (data[ year_born ]==1990)] data[(data[ column_1 ]== french ) & (data[ year_born ]==1990) & ~(data[ city ]== London )]復(fù)制代碼
通過邏輯運(yùn)算來取數(shù)據(jù)子集。要使用 & (AND)、 ~ (NOT) 和 | (OR),必須在邏輯運(yùn)算前后加上「and」。
data[data[ column_1 ].isin([ french , english ])]復(fù)制代碼
除了可以在同一列使用多個 OR,你還可以使用.isin() 函數(shù)。
基本繪圖
matplotlib 包使得這項功能成為可能。正如我們在介紹中所說,它可以直接在 pandas 中使用。
data[ column_numerical ].plot()復(fù)制代碼

().plot() 輸出的示例
data[ column_numerical ].hist()復(fù)制代碼
畫出數(shù)據(jù)分布(直方圖)

.hist() 輸出的示例
%matplotlib inline復(fù)制代碼
如果你在使用 Jupyter,不要忘記在畫圖之前加上以上代碼。
更新數(shù)據(jù)
data.loc[8, column_1 ] = english 將第八行名為 column_1 的列替換為「english」復(fù)制代碼
data.loc[data[ column_1 ]== french , column_1 ] = French復(fù)制代碼
在一行代碼中改變多列的值
好了,現(xiàn)在你可以做一些在 excel 中可以輕松訪問的事情了。下面讓我們深入研究 excel 中無法實現(xiàn)的一些令人驚奇的操作吧。
中級函數(shù)
統(tǒng)計出現(xiàn)的次數(shù)
data[ column_1 ].value_counts()復(fù)制代碼

.value_counts() 函數(shù)輸出示例
在所有的行、列或者全數(shù)據(jù)上進(jìn)行操作
data[ column_1 ].map(len)復(fù)制代碼
len() 函數(shù)被應(yīng)用在了「column_1」列中的每一個元素上
.map() 運(yùn)算給一列中的每一個元素應(yīng)用一個函數(shù)
data[ column_1 ].map(len).map(lambda x: x/100).plot()復(fù)制代碼
pandas 的一個很好的功能就是鏈?zhǔn)椒椒ǎ╰omaugspurger.github.io/method-chai… 和.plot())。
data.apply(sum)復(fù)制代碼
.apply() 會給一個列應(yīng)用一個函數(shù)。
.applymap() 會給表 (DataFrame) 中的所有單元應(yīng)用一個函數(shù)。
tqdm, 唯一的
在處理大規(guī)模數(shù)據(jù)集時,pandas 會花費(fèi)一些時間來進(jìn)行.map()、.apply()、.applymap() 等操作。tqdm 是一個可以用來幫助預(yù)測這些操作的執(zhí)行何時完成的包(是的,我說謊了,我之前說我們只會使用到 pandas)。
from tqdm import tqdm_notebook tqdm_notebook().pandas()復(fù)制代碼
用 pandas 設(shè)置 tqdm
data[ column_1 ].progress_map(lambda x: x.count( e ))復(fù)制代碼
用 .progress_map() 代替.map()、.apply() 和.applymap() 也是類似的。

在 Jupyter 中使用 tqdm 和 pandas 得到的進(jìn)度條
相關(guān)性和散射矩陣
data.corr() data.corr().applymap(lambda x: int(x*100)/100)復(fù)制代碼

.corr() 會給出相關(guān)性矩陣
pd.plotting.scatter_matrix(data, figsize=(12,8))復(fù)制代碼

散點(diǎn)矩陣的例子。它在同一幅圖中畫出了兩列的所有組合。
pandas 中的高級操作
The SQL 關(guān)聯(lián)
在 pandas 中實現(xiàn)關(guān)聯(lián)是非常非常簡單的
data.merge(other_data, on=[ column_1 , column_2 , column_3 ])復(fù)制代碼
關(guān)聯(lián)三列只需要一行代碼
分組
一開始并不是那么簡單,你首先需要掌握語法,然后你會發(fā)現(xiàn)你一直在使用這個功能。
data.groupby( column_1 )[ column_2 ].apply(sum).reset_index()復(fù)制代碼
按一個列分組,選擇另一個列來執(zhí)行一個函數(shù)。.reset_index() 會將數(shù)據(jù)重構(gòu)成一個表。

正如前面解釋過的,為了優(yōu)化代碼,在一行中將你的函數(shù)連接起來。
行迭代
dictionary = {} for i,row in data.iterrows(): dictionary[row[ column_1 ]] = row[ column_2 ]復(fù)制代碼
.iterrows() 使用兩個變量一起循環(huán):行索引和行的數(shù)據(jù) (上面的 i 和 row)
總而言之,pandas 是 python 成為出色的編程語言的原因之一
我本可以展示