目錄
(4-1)
讀
- read_csv
- read_excel
- read_hdf
- read_sql
- read_json
- read_msgpack (experimental)
- read_html
- read_gbq (experimental)
- read_stata
- read_sas
- read_clipboard
- read_pickle
寫
- to_csv
- to_excel
- to_hdf
- to_sql
- to_json
- to_msgpack (experimental)
- to_html
- to_gbq (experimental)
- to_stata
- to_clipboard
- to_pickle
其它數據源:
- 數據庫
- 讀取網頁數據,網絡文件
- 讀取股票數據
- yahoo,世界銀行等等、
import numpy as np
import pandas as pd
pd.set_option('display.max_columns', 10)
pd.set_option('display.max_rows', 10)
- 分組
- 分組數據統計
- Matplotlib以及pandas數據可視化初步
groupby 基礎(4-1)
1,引用讀取數據
url="https://en.wikipedia.org/wiki/List_of_European_Cup_and_UEFA_Champions_League_finals"
eu_champions=pd.read_html(url)
使用multiple index進行統計
使用multiple index進行統計
aggregate函數
stat_Grp.aggregate(np.sum)
數據可視化工具(4-2)
網站:https://matplotlib.org/api/pyplot_api.html
數據網站:http://hao.bigdata.ren/
- matplotilb :pip install matplotlib
- seaborn
- PyX
- Boken
- .....
導入相關庫
matplotlib中數據可視化
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
例子1
x=np.arange(-10,10,0.005)
y=x**2+1
plt.xlim(-10,10)
plt.plot(x,y)
plt.show()
例子2
x=np.arange(-1,1,0.005)
y=(1+x)/(1-x)
plt.xlim(-1,1)
plt.plot(x,y)
plt.show()
例子3
x=np.arange(-1,1,0.005)
y1=np.abs(250*x)
y2=(1+x)/(1-x)
plt.plot(x,y1,label='First one')
plt.plot(x,y2,label='Second one')
plt.xlabel('x')
plt.ylabel('y')
plt.title('My Figure')
plt.legend()
plt.show()
Pandas中的數據可視化(7-1)
1,plot()函數 --折線圖
normals = pd.Series(np.random.normal(size=10))
normals.plot()
2求和
normals.cumsum().plot(grid=True)
3,繪圖
1,一張圖,不同顏色數據顯示(顯示一張圖,3個數據對比)
variables = pd.DataFrame({'normal': np.random.normal(size=100),
'gamma': np.random.gamma(1, size=100),
'poisson': np.random.poisson(size=100)})
variables.cumsum(0).plot()
---------------
2,獨立繪圖(顯示3個圖)
variables.cumsum(0).plot(subplots=True)
2,Bar plots-條形圖
條形圖通常用來顯示或者比較數值,比如個數,數量。pandas裏面利用`kind='bar'`參數繪製條形圖
1,豎化-生還數
titanic.groupby('Pclass').Survived.sum().plot(kind='bar')
2,橫着化
titanic.groupby(['Sex','Pclass']).Survived.sum().plot(kind='barh')
3,直方圖hist()
titanic.Fare.hist(grid=False)
titanic.Fare.hist(bins=30)
4,Boxplot
titanic.boxplot(column='Fare', by='Pclass', grid=False)
5,散點圖scatter( )
plt.scatter(baseball.ab, baseball.h)
plt.xlim(0, 700); plt.ylim(0, 200)
2,點的大小
plt.scatter(baseball.ab, baseball.h, s=baseball.hr*10, alpha=0.5)
plt.xlim(0, 700); plt.ylim(0, 200)
3,不同顏色
plt.scatter(baseball.ab, baseball.h, c=baseball.hr, s=40, cmap='hot')
plt.xlim(0, 700); plt.ylim(0, 200);
6.matplotlib 各種繪圖參數
1,顯示,實線和虛線
plt.plot(X, C, color="blue", linewidth=3.0, linestyle="--")
plt.plot(X, S, color="green", linewidth=6.0, linestyle="-")
7.扇形圖
ax = plt.axes([0,0,1,1], polar=True)
N = 20
theta = np.arange(0.0, 2*np.pi, 2*np.pi/N)
radii = 10*np.random.rand(N)
width = np.pi/4*np.random.rand(N)
bars = plt.bar(theta, radii, width=width, bottom=0.0)
for r,bar in zip(radii, bars):
bar.set_facecolor( plt.cm.jet(r/10.))
bar.set_alpha(0.5)
ax.set_xticklabels([])
ax.set_yticklabels([])