一個在線 Python
編程環境,每行的代碼在敲擊 Shift + Enter
後單獨執行,便於初學者快速掌握編程語言。 Jupyter 能夠完美利用 Python 豐富的數據分析和科學計算的工具庫,實現一些非大數據的分析計算需求。javascript
用於操做行列數據,方便地實現各類數據分析的形式。html
因爲 Python 自己的限制,若是須要計算的數據太大,而沒法一次載入內存,則須要分塊導入數據,並對查詢作相應的修改。java
(現充請跳過本節,直接訪問線上地址,點擊 Welcome to Python.ipynb
)git
在命令行中輸入:github
pip install jupyterpip install pandaspip install matplotlib
cd到指定目錄,啓動:數據庫
jupyter notebook
在 Jupyter Notebook 中,Pandas 是操做數據的工具,matplotlib 是執行做圖的工具。編程
在 Cell 中輸入並執行:api
import pandas as pdimport matplotlib.pyplot as plt
在 Cell 中輸入並執行:數組
df = pd.read_csv('./data.csv', index_col='id')
因爲在線環境沒法建立文件,也能夠執行如下語句建立一個 DataFrame
:數據結構
dates = pd.date_range('20130101', periods=6)df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
index_col
的做用是索引,是爲了高效查詢創建的特殊數據結構,簡單說(不許確)是黃頁
如今想象你手邊有Excel(固然你也能夠真的開一個)顯示以下表格:
date(索引) | A | B | C | D |
---|---|---|---|---|
2013-01-01 | 0.469112 | -0.282863 | -1.509059 | -1.135632 |
2013-01-02 | 1.212112 | -0.173215 | 0.119209 | -1.044236 |
2013-01-03 | -0.861849 | -2.104569 | -0.494929 | 1.071804 |
2013-01-04 | 0.721555 | -0.706771 | -1.039575 | 0.271860 |
2013-01-05 | -0.424972 | 0.567020 | 0.276232 | -1.087401 |
2013-01-06 | -0.673690 | 0.113648 | -1.478427 | 0.524988 |
行的選取
在 Cell 中輸入並執行:
rows = df[0:3]
選擇第0行至第3行,結果如灰色區域所示:
date(索引) | A | B | C | D |
---|---|---|---|---|
2013-01-01 | 0.469112 | -0.282863 | -1.509059 | -1.135632 |
2013-01-02 | 1.212112 | -0.173215 | 0.119209 | -1.044236 |
2013-01-03 | -0.861849 | -2.104569 | -0.494929 | 1.071804 |
2013-01-04 | 0.721555 | -0.706771 | -1.039575 | 0.271860 |
2013-01-05 | -0.424972 | 0.567020 | 0.276232 | -1.087401 |
2013-01-06 | -0.673690 | 0.113648 | -1.478427 | 0.524988 |
2.列的選取
cols = df[['A', 'B', 'C']]
選擇列A,B,C,結果如灰色區域所示:
date(索引) | A | B | C | D |
---|---|---|---|---|
2013-01-01 | 0.469112 | -0.282863 | -1.509059 | -1.135632 |
2013-01-02 | 1.212112 | -0.173215 | 0.119209 | -1.044236 |
2013-01-03 | -0.861849 | -2.104569 | -0.494929 | 1.071804 |
2013-01-04 | 0.721555 | -0.706771 | -1.039575 | 0.271860 |
2013-01-05 | -0.424972 | 0.567020 | 0.276232 | -1.087401 |
2013-01-06 | -0.673690 | 0.113648 | -1.478427 | 0.524988 |
3.塊的選取
df.loc['20130102':'20130104',['A','B']]
選擇行和列組成的數據塊,結果如灰色區域所示:
date(索引) | A | B | C | D |
---|---|---|---|---|
2013-01-01 | 0.469112 | -0.282863 | -1.509059 | -1.135632 |
2013-01-02 | 1.212112 | -0.173215 | 0.119209 | -1.044236 |
2013-01-03 | -0.861849 | -2.104569 | -0.494929 | 1.071804 |
2013-01-04 | 0.721555 | -0.706771 | -1.039575 | 0.271860 |
2013-01-05 | -0.424972 | 0.567020 | 0.276232 | -1.087401 |
2013-01-06 | -0.673690 | 0.113648 | -1.478427 | 0.524988 |
Pandas 中的基本數據結構有二,Series
和 Dataframe
。 Series
用來建立行,也能夠理解爲一維數組。 Dataframe
用來建立塊,或稱爲矩陣,表格。
建立一個數組[1,1,2,3,5]:
s = pd.Series([1,1,2,3,5])
建立一個 6x4
的表格塊,單元格內容爲隨機數列名爲 A,B,C,D。
pd.DataFrame(np.random.randn(6,4), columns=list('ABCD'))
從已有的列建立一個新的列
df['sumAB'] = pd.Series(df['A'] + df['B'], index=df.index)df['10A'] = pd.Series(df['A']*10, index=df.index)
df['A'] + df['B']
表示兩列對應單元格的相加
df['A']*10
表示列A每一個單元格 *10
運算後df的值以下:
date(索引) | A | B | C | D | sumAB | 10A |
---|---|---|---|---|---|---|
2013-01-01 | 0.469112 | -0.282863 | -1.509059 | -1.135632 | 0.186249 | 4.69112 |
2013-01-02 | 1.212112 | -0.173215 | 0.119209 | -1.044236 | 1.038897 | 12.12112 |
2013-01-03 | -0.861849 | -2.104569 | -0.494929 | 1.071804 | -2.966418 | -8.61849 |
2013-01-04 | 0.721555 | -0.706771 | -1.039575 | 0.271860 | 0.014784 | 7.21555 |
2013-01-05 | -0.424972 | 0.567020 | 0.276232 | -1.087401 | 0.142048 | -4.24972 |
2013-01-06 | -0.673690 | 0.113648 | -1.478427 | 0.524988 | -0.560042 | -6.73690 |
在方括號中加入判斷條件來過濾行,條件必需返回 True
或者 False
df[(df.index >= '2013-01-01') & (df.index <= '2013-01-03')] df[df['A'] > 0]
爲了快速瞭解數據的結構,一些值得掌握的指令以下:
# 查看錶頭5行
df.head(5)
# 查看錶末5行 df.tail(5) # 查看列的名字 df.columns # 查看錶格當前的值 df.values # 查看全部列的統計描述,包括平均值,標準差,最大最小值,以及25%,50%,75%的 percentile 值 df.describe() # 對錶按照A列升序排序 df.sort_values(by=’A’)
Pandas 與 matplotlib 配合使用,能夠支持幾乎全部經常使用的圖表形式,這裏以經常使用的直方圖爲例,來觀察一個典型的獨立隨機變量的正態分佈:
# 首先打開圖表行內顯示%matplotlib inline# 生成600個隨機數(符合正態分佈),存放在 Series 或 DataFrame 的某一列中nd = pd.Series(np.random.randn(600))# bins 表示直方圖的方塊數# range 表示圖表顯示的範圍nd.hist(bins=100, range=(-5,5))
結果如圖所示:
通過了這幾節,想必現充們僅僅學會了例子中的代碼,若是想進一步瞭解,請參考如下文檔:
進一步的使用須要對 Python 和 Pandas 作全面的瞭解,能夠參考如下書籍:
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */ var disqus_shortname = 'chenhfblog'; // required: replace example with your forum shortname /* * * DON'T EDIT BELOW THIS LINE * * */ (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();
<div class="footer"> <div class="contact"> <p> GITHUB <br> josherichchenATgmail.com </p> </div> <div class="contact"> <p> <a href="https://github.com/josherich">github.com/josherich</a> <br> </p> </div> </div>