一氮发、Series類型
由一組數(shù)據(jù)及與之相關(guān)的數(shù)據(jù)索引組成籍胯。
從標(biāo)量值創(chuàng)建
import pandas as pd
a = pd.Series([9, 8, 7, 6], index=['a', 'b', 'c', 'd'])
a
Out[13]:
a 9
b 8
c 7
d 6
dtype: int64
從字典類型創(chuàng)建
import pandas as pd
b = pd.Series({'a':9, 'b':8, 'c':7})
b
Out[16]:
a 9
b 8
c 7
dtype: int64
c = pd.Series({'a':9, 'b':8, 'c':7}, index=['c', 'a', 'b', 'd'])
c
Out[18]:
c 7.0
a 9.0
b 8.0
d NaN
dtype: float64
從ndarray類型創(chuàng)建
import pandas as pd
import numpy as np
n = pd.Series(np.arange(5))
n
Out[22]:
0 0
1 1
2 2
3 3
4 4
dtype: int32
m = pd.Series(np.arange(5), index=np.arange(9, 4, -1))
m
Out[24]:
9 0
8 1
7 2
6 3
5 4
dtype: int32
Series類型的基本操作
- .index 獲取索引
- .values 獲取數(shù)據(jù)
- ... in ... 判斷是否在自定義索引中
import pandas as pd
b = pd.Series([9, 8, 7, 6], ['a', 'b' ,'c', 'd'])
b
Out[27]:
a 9
b 8
c 7
d 6
dtype: int64
b.index ###############################################
Out[28]: Index(['a', 'b', 'c', 'd'], dtype='object')
b.values #########################################################
Out[29]: array([9, 8, 7, 6], dtype=int64)
b['b']
Out[30]: 8
b[1]
Out[31]: 8
b[['c', 'd', 0]]
Out[32]:
c 7.0
d 6.0
0 NaN
dtype: float64
b[['c', 'd', 'a']]
Out[33]:
c 7
d 6
a 9
dtype: int64
b[:3]
Out[34]:
a 9
b 8
c 7
dtype: int64
b[b > b.median()]
Out[35]:
a 9
b 8
dtype: int64
np.exp(b)
Out[36]:
a 8103.083928
b 2980.957987
c 1096.633158
d 403.428793
dtype: float64
'c' in b #################################
Out[37]: True
0 in b
Out[38]: False
b.get('f', 100)
Out[39]: 100
Series類型對(duì)齊操作
把索引相同的項(xiàng)進(jìn)行相加
import pandas as pd
a = pd.Series([1, 2, 3],['c', 'd', 'e'])
b = pd.Series([9, 8, 7, 6], ['a', 'b', 'c', 'd'])
a + b
Out[47]:
a NaN
b NaN
c 8.0
d 8.0
e NaN
dtype: float64
Series類型的name屬性
Series對(duì)象和索引都可以有一個(gè)名字,儲(chǔ)存在屬性.name中景殷。
import pandas as pd
b = pd.Series([9, 8, 7, 6], ['a', 'b', 'c', 'd'])
b.name = 'Series對(duì)象'
b.index.name = '索引列'
b
Out[52]:
索引列
a 9
b 8
c 7
d 6
Name: Series對(duì)象, dtype: int64
Series類型的修改
Series對(duì)象可以隨時(shí)修改并立刻生效
import pandas as pd
b = pd.Series([9, 8, 7, 6], ['a', 'b', 'c', 'd'])
b.name = 'Series對(duì)象'
b.index.name = '索引列'
b
Out[52]:
索引列
a 9
b 8
c 7
d 6
Name: Series對(duì)象, dtype: int64
b.name = 'aaa'
b['b', 'c'] = 10
b
Out[55]:
索引列
a 9
b 10
c 10
d 6
Name: aaa, dtype: int64
二溅呢、Pandas庫(kù)的DataFrame類型
DataFrame類型
由共用相同索引的一組列組成澡屡。(相當(dāng)于表格)
- 表格型的數(shù)據(jù)類型,每列值類型可以不同咐旧。
- 行索引(index)驶鹉、列索引(column)。
- 常用于表達(dá)二維數(shù)據(jù)铣墨,但可以表達(dá)多維數(shù)據(jù)室埋。
從二維ndarray對(duì)象字典創(chuàng)建
import pandas as pd
import numpy as np
d = pd.DataFrame(np.arange(10).reshape(2, 5))
d
Out[4]:
0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
從一維ndarray對(duì)象字典創(chuàng)建
import pandas as pd
dt = {'one':pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two':pd.Series([9, 8, 7, 6], index=['a', 'b', 'c', 'd'])}
d = pd.DataFrame(dt)
d
Out[11]:
one two
a 1.0 9
b 2.0 8
c 3.0 7
d NaN 6
pd.DataFrame(dt, index=['b', 'c', 'd'], columns=['two', 'three'])
Out[12]:
two three
b 8 NaN
c 7 NaN
d 6 NaN
從列表類型的字典創(chuàng)建
import pandas as pd
dl = {'one':[1, 2, 3, 4], 'two':[9, 8, 7, 6]}
d = pd.DataFrame(dl, index=['a', 'b', 'c', 'd'])
d
Out[16]:
one two
a 1 9
b 2 8
c 3 7
d 4 6
d.index
Out[17]: Index(['a', 'b', 'c', 'd'], dtype='object')
d.columns
Out[18]: Index(['one', 'two'], dtype='object')
d.values
Out[19]:
array([[1, 9],
[2, 8],
[3, 7],
[4, 6]], dtype=int64)
d['one']
Out[20]:
a 1
b 2
c 3
d 4
Name: one, dtype: int64
d.ix['b']
Out[21]:
one 2
two 8
Name: b, dtype: int64
d['one']['b']
Out[22]: 2
三、Pandas庫(kù)的數(shù)據(jù)類型操作
改變Series和DataFrame對(duì)象
增加或重排:重新索引
import pandas as pd
dl = {'城市':['北京', '上海', '廣州', '深圳', '沈陽(yáng)'],
'環(huán)比':[101.5, 101.2, 101.3, 101.4, 101.5],
'同比':[120.7, 127.3, 119.4, 140.9, 101.4],
'定基':[121.4, 127.8, 120.2, 145.5, 101.6]}
d = pd.DataFrame(dl, index=['c1', 'c2', 'c3', 'c4', 'c5'])
d
Out[6]:
同比 城市 定基 環(huán)比
c1 120.7 北京 121.4 101.5
c2 127.3 上海 127.8 101.2
c3 119.4 廣州 120.2 101.3
c4 140.9 深圳 145.5 101.4
c5 101.4 沈陽(yáng) 101.6 101.5
d = d.reindex(index=['c5', 'c4', 'c3', 'c2', 'c1'])
d
Out[8]:
同比 城市 定基 環(huán)比
c5 101.4 沈陽(yáng) 101.6 101.5
c4 140.9 深圳 145.5 101.4
c3 119.4 廣州 120.2 101.3
c2 127.3 上海 127.8 101.2
c1 120.7 北京 121.4 101.5
d = d.reindex(columns=['城市', '同比', '環(huán)比', '定基'])
d
Out[10]:
城市 同比 環(huán)比 定基
c5 沈陽(yáng) 101.4 101.5 101.6
c4 深圳 140.9 101.4 145.5
c3 廣州 119.4 101.3 120.2
c2 上海 127.3 101.2 127.8
c1 北京 120.7 101.5 121.4
.reindex(index=None,columns=None,...)的參數(shù)
參數(shù) |
說明 |
index,columns |
新的行列自定義索引 |
fill_value |
重新索引中伊约,用于填充缺失位置的值 |
method |
填充方法姚淆,ffill當(dāng)前值向前填充,bfill向后填充 |
limit |
最大填充量 |
copy |
默認(rèn)True屡律,生成新的對(duì)象腌逢,F(xiàn)alse時(shí),新舊相等不復(fù)制 |
import pandas as pd
dl = {'城市':['北京', '上海', '廣州', '深圳', '沈陽(yáng)'],
'環(huán)比':[101.5, 101.2, 101.3, 101.4, 101.5],
'同比':[120.7, 127.3, 119.4, 140.9, 101.4],
'定基':[121.4, 127.8, 120.2, 145.5, 101.6]}
d = pd.DataFrame(dl, index=['c1', 'c2', 'c3', 'c4', 'c5'])
d
Out[17]:
同比 城市 定基 環(huán)比
c1 120.7 北京 121.4 101.5
c2 127.3 上海 127.8 101.2
c3 119.4 廣州 120.2 101.3
c4 140.9 深圳 145.5 101.4
c5 101.4 沈陽(yáng) 101.6 101.5
newc = d.columns.insert(4,'新增')
newb = d.reindex(columns=newc, fill_value=200)
newb
Out[20]:
同比 城市 定基 環(huán)比 新增
c1 120.7 北京 121.4 101.5 200
c2 127.3 上海 127.8 101.2 200
c3 119.4 廣州 120.2 101.3 200
c4 140.9 深圳 145.5 101.4 200
c5 101.4 沈陽(yáng) 101.6 101.5 200
索引類型的常用方法
方法 |
說明 |
.append(idx) |
連接另一個(gè)index對(duì)象超埋,產(chǎn)生新的index對(duì)象 |
.diff(idx) |
計(jì)算差集搏讶,產(chǎn)生新的index對(duì)象 |
.intersection(idx) |
計(jì)算交集 |
.union(idx) |
計(jì)算并集 |
.delete(loc) |
刪除loc位置處的元素 |
.insert(loc,e) |
在loc位置增加一個(gè)元素e |
d
Out[21]:
同比 城市 定基 環(huán)比
c1 120.7 北京 121.4 101.5
c2 127.3 上海 127.8 101.2
c3 119.4 廣州 120.2 101.3
c4 140.9 深圳 145.5 101.4
c5 101.4 沈陽(yáng) 101.6 101.5
nc = d.columns.delete(2)
ni = d.index.insert(5, 'c6')
nd = d.reindex(index=ni, columns=nc, method='ffill')
nd
Out[25]:
同比 城市 環(huán)比
c1 120.7 北京 101.5
c2 127.3 上海 101.2
c3 119.4 廣州 101.3
c4 140.9 深圳 101.4
c5 101.4 沈陽(yáng) 101.5
c6 101.4 沈陽(yáng) 101.5
刪除指定索引對(duì)象
.drop()能刪除Series和DataFrame指定行或列索引
a = pd.Series([9, 8, 7, 6], index=['a', 'b', 'c', 'd'])
a
Out[27]:
a 9
b 8
c 7
d 6
dtype: int64
a.drop(['b', 'c'])
Out[28]:
a 9
d 6
dtype: int64
d
Out[29]:
同比 城市 定基 環(huán)比
c1 120.7 北京 121.4 101.5
c2 127.3 上海 127.8 101.2
c3 119.4 廣州 120.2 101.3
c4 140.9 深圳 145.5 101.4
c5 101.4 沈陽(yáng) 101.6 101.5
d.drop('c5')
Out[30]:
同比 城市 定基 環(huán)比
c1 120.7 北京 121.4 101.5
c2 127.3 上海 127.8 101.2
c3 119.4 廣州 120.2 101.3
c4 140.9 深圳 145.5 101.4
d.drop('定基', axis=1)
Out[31]:
同比 城市 環(huán)比
c1 120.7 北京 101.5
c2 127.3 上海 101.2
c3 119.4 廣州 101.3
c4 140.9 深圳 101.4
c5 101.4 沈陽(yáng) 101.5
四、Pandas庫(kù)的數(shù)據(jù)類型運(yùn)算
算術(shù)運(yùn)算法則
- 算術(shù)運(yùn)算根據(jù)行列索引纳本,補(bǔ)齊后運(yùn)算窍蓝,運(yùn)算默認(rèn)產(chǎn)生浮點(diǎn)數(shù)
- 補(bǔ)齊使缺項(xiàng)填充NaN
- 二維和一維、一維和零維間為廣播運(yùn)算
- 采用+ - * /符號(hào)進(jìn)行的二元運(yùn)算產(chǎn)生新的對(duì)象
import pandas as pd
import numpy as np
a = pd.DataFrame(np.arange(12).reshape(3,4))
a
Out[37]:
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
b = pd.DataFrame(np.arange(20).reshape(4,5))
b
Out[39]:
0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
3 15 16 17 18 19
a + b
Out[40]:
0 1 2 3 4
0 0.0 2.0 4.0 6.0 NaN
1 9.0 11.0 13.0 15.0 NaN
2 18.0 20.0 22.0 24.0 NaN
3 NaN NaN NaN NaN NaN
a * b
Out[41]:
0 1 2 3 4
0 0.0 1.0 4.0 9.0 NaN
1 20.0 30.0 42.0 56.0 NaN
2 80.0 99.0 120.0 143.0 NaN
3 NaN NaN NaN NaN NaN
數(shù)據(jù)類型的算術(shù)運(yùn)算方法形式的運(yùn)算
方法 |
說明 |
.add(d, **argws) |
類型間加法運(yùn)算繁成,可選參數(shù) |
.sub(d, **argws) |
類型間減法運(yùn)算吓笙,可選參數(shù) |
.mul(d, **argws) |
類型間乘法運(yùn)算,可選參數(shù) |
.div(d, **argws) |
類型間除法運(yùn)算巾腕,可選參數(shù) |
b.add(a, fill_value=100)
Out[42]:
0 1 2 3 4
0 0.0 2.0 4.0 6.0 104.0
1 9.0 11.0 13.0 15.0 109.0
2 18.0 20.0 22.0 24.0 114.0
3 115.0 116.0 117.0 118.0 119.0
a.mul(b, fill_value=0)
Out[43]:
0 1 2 3 4
0 0.0 1.0 4.0 9.0 0.0
1 20.0 30.0 42.0 56.0 0.0
2 80.0 99.0 120.0 143.0 0.0
3 0.0 0.0 0.0 0.0 0.0
- 不同緯度間為廣播運(yùn)算面睛,一維Series默認(rèn)在軸1參與運(yùn)算
import pandas as pd
import numpy as np
b = pd.DataFrame(np.arange(20).reshape(4,5))
b
Out[47]:
0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
3 15 16 17 18 19
c = pd.Series(np.arange(4))
c
Out[50]:
0 0
1 1
2 2
3 3
dtype: int32
c - 10
Out[51]:
0 -10
1 -9
2 -8
3 -7
dtype: int32
b - c
Out[52]:
0 1 2 3 4
0 0.0 0.0 0.0 0.0 NaN
1 5.0 5.0 5.0 5.0 NaN
2 10.0 10.0 10.0 10.0 NaN
3 15.0 15.0 15.0 15.0 NaN
- 下方法可以令一維Series參與軸0運(yùn)算
b
Out[53]:
0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
3 15 16 17 18 19
c
Out[54]:
0 0
1 1
2 2
3 3
dtype: int32
b.sub(c, axis=0)
Out[55]:
0 1 2 3 4
0 0 1 2 3 4
1 4 5 6 7 8
2 8 9 10 11 12
3 12 13 14 15 16
比較運(yùn)算
- 比較運(yùn)算只能比較相同索引的元素,不進(jìn)行補(bǔ)齊
- 二維和一維尊搬、一維和零位間為廣播運(yùn)算
- 采用> < >= <= == !=等符號(hào)進(jìn)行的二元運(yùn)算產(chǎn)生布爾對(duì)象
import pandas as pd
import numpy as np
a = pd.DataFrame(np.arange(12).reshape(3,4))
a
Out[59]:
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
d = pd.DataFrame(np.arange(12, 0, -1).reshape(3,4))
d
Out[62]:
0 1 2 3
0 12 11 10 9
1 8 7 6 5
2 4 3 2 1
a > d
Out[63]:
0 1 2 3
0 False False False False
1 False False False True
2 True True True True
- 不同緯度叁鉴,廣播運(yùn)算,默認(rèn)1軸
import numpy as np
import pandas as pd
a = pd.DataFrame(np.arange(12).reshape(3,4))
a
Out[67]:
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
c = pd.Series(np.arange(4))
c
Out[69]:
0 0
1 1
2 2
3 3
dtype: int32
a>c
Out[70]:
0 1 2 3
0 False False False False
1 True True True True
2 True True True True
c>0
Out[71]:
0 False
1 True
2 True
3 True
dtype: bool
五佛寿、數(shù)據(jù)的排序
根據(jù)索引排序
- sort_index()方法在指定軸上根據(jù)索引進(jìn)行排列幌墓,默認(rèn)升序。
.sort_index(axis=0,ascending=True) 默認(rèn)0軸
import pandas as pd
import numpy as np
b = pd.DataFrame(np.arange(20).reshape(4,5), index=['c', 'a', 'd', 'b'])
b
Out[75]:
0 1 2 3 4
c 0 1 2 3 4
a 5 6 7 8 9
d 10 11 12 13 14
b 15 16 17 18 19
b.sort_index()
Out[77]:
0 1 2 3 4
a 5 6 7 8 9
b 15 16 17 18 19
c 0 1 2 3 4
d 10 11 12 13 14
b.sort_index(ascending=False)
Out[78]:
0 1 2 3 4
d 10 11 12 13 14
c 0 1 2 3 4
b 15 16 17 18 19
a 5 6 7 8 9
b.sort_index(axis=1,ascending=False)
Out[79]:
4 3 2 1 0
c 4 3 2 1 0
a 9 8 7 6 5
d 14 13 12 11 10
b 19 18 17 16 15
根據(jù)數(shù)據(jù)排序
- .sort_values()方法在指定軸上根據(jù)數(shù)值進(jìn)行排序冀泻,默認(rèn)升序常侣。
- NaN默認(rèn)放在末尾
- Series.sort_values(axis=0,ascending=True)
- DateFrame.sort_values(by,axis=0,ascending=Ture)
import pandas as pd
import numpy as np
b = pd.DataFrame(np.arange(20).reshape(4,5), index=['c', 'a', 'd', 'b'])
b
Out[75]:
0 1 2 3 4
c 0 1 2 3 4
a 5 6 7 8 9
d 10 11 12 13 14
b 15 16 17 18 19
c = b.sort_values(2, ascending=False)
c
Out[82]:
0 1 2 3 4
b 15 16 17 18 19
d 10 11 12 13 14
a 5 6 7 8 9
c 0 1 2 3 4
c = c.sort_values('a', axis=1, ascending=False)
c
Out[84]:
4 3 2 1 0
b 19 18 17 16 15
d 14 13 12 11 10
a 9 8 7 6 5
c 4 3 2 1 0
六、數(shù)據(jù)的基本統(tǒng)計(jì)分析
基本的統(tǒng)計(jì)分析函數(shù)
適用于Series和DataFrame類型
方法 |
說明 |
.sum() |
計(jì)算數(shù)據(jù)的總和弹渔,按0軸計(jì)算胳施,下同 |
.count() |
非NaN值的數(shù)量 |
.mean() .median() |
計(jì)算數(shù)據(jù)的算術(shù)平均值、算術(shù)中位數(shù) |
.var() .std() |
計(jì)算數(shù)據(jù)的方差肢专、標(biāo)準(zhǔn)差 |
.min() .max() |
計(jì)算數(shù)據(jù)的最小值舞肆、最大值 |
適用于Series類型
方法 |
說明 |
.argmin() .argmax() |
計(jì)算數(shù)據(jù)最大值焦辅、最小值所在位置的索引位置(自動(dòng)索引) |
.idxmin() .idxmax() |
計(jì)算數(shù)據(jù)最大值、最小值所在位置的索引(自定義索引) |
適用于Series和DataFrame類型(包括上訴所有)
方法 |
說明 |
.describe() |
針對(duì)0軸(各列)的統(tǒng)計(jì)匯總 |
import pandas as pd
a = pd.Series([9, 8, 7, 6], index=['a', 'b', 'c', 'd'])
a
Out[87]:
a 9
b 8
c 7
d 6
dtype: int64
a.describe()
Out[88]:
count 4.000000
mean 7.500000
std 1.290994
min 6.000000
25% 6.750000
50% 7.500000
75% 8.250000
max 9.000000
dtype: float64
import pandas as pd
import numpy as np
b = pd.DataFrame(np.arange(20).reshape(4,5), index=['c', 'a', 'd', 'b'])
b.describe()
Out[92]:
0 1 2 3 4
count 4.000000 4.000000 4.000000 4.000000 4.000000
mean 7.500000 8.500000 9.500000 10.500000 11.500000
std 6.454972 6.454972 6.454972 6.454972 6.454972
min 0.000000 1.000000 2.000000 3.000000 4.000000
25% 3.750000 4.750000 5.750000 6.750000 7.750000
50% 7.500000 8.500000 9.500000 10.500000 11.500000
75% 11.250000 12.250000 13.250000 14.250000 15.250000
max 15.000000 16.000000 17.000000 18.000000 19.000000
b.describe().ix['max']
Out[93]:
0 15.0
1 16.0
2 17.0
3 18.0
4 19.0
Name: max, dtype: float64
b.describe()[2]
Out[94]:
count 4.000000
mean 9.500000
std 6.454972
min 2.000000
25% 5.750000
50% 9.500000
75% 13.250000
max 17.000000
Name: 2, dtype: float64
七椿胯、數(shù)據(jù)的累計(jì)統(tǒng)計(jì)分析
累計(jì)統(tǒng)計(jì)分析函數(shù)
適用于Series和DataFrame類型
方法 |
說明 |
.cumsum() |
依次給出前1筷登、2、...压状、n個(gè)數(shù)的和 |
.cumprod() |
依次給出前1仆抵、2跟继、...种冬、n個(gè)數(shù)的積 |
.cummax() |
依次給出前1、2舔糖、...娱两、n個(gè)數(shù)的最大值 |
.cummin() |
依次給出前1、2金吗、...十兢、n個(gè)數(shù)的最小值 |
import pandas as pd
import numpy as np
b = pd.DataFrame(np.arange(20).reshape(4,5), index=['c', 'a', 'd', 'b'])
b
Out[98]:
0 1 2 3 4
c 0 1 2 3 4
a 5 6 7 8 9
d 10 11 12 13 14
b 15 16 17 18 19
b.cumsum()
Out[99]:
0 1 2 3 4
c 0 1 2 3 4
a 5 7 9 11 13
d 15 18 21 24 27
b 30 34 38 42 46
適用于適用于Series和DataFrame類型,滾動(dòng)計(jì)算(窗口計(jì)算)
方法 |
說明 |
.rolling(w).sum() |
依次計(jì)算相鄰w個(gè)元素的和 |
.rolling(w).mean() |
依次計(jì)算相鄰w個(gè)元素的算術(shù)平均數(shù) |
.rolling(w).var() |
依次計(jì)算相鄰w個(gè)元素的方差 |
.rolling(w).std() |
依次計(jì)算相鄰w個(gè)元素的標(biāo)準(zhǔn)差 |
.rolling(w).min() .max() |
依次計(jì)算相鄰w個(gè)元素的最大值或最小值 |
import pandas as pd
import numpy as np
b = pd.DataFrame(np.arange(20).reshape(4,5), index=['c', 'a', 'd', 'b'])
b
Out[98]:
0 1 2 3 4
c 0 1 2 3 4
a 5 6 7 8 9
d 10 11 12 13 14
b 15 16 17 18 19
b.cumsum()
Out[99]:
0 1 2 3 4
c 0 1 2 3 4
a 5 7 9 11 13
d 15 18 21 24 27
b 30 34 38 42 46
b.rolling(2).sum()
Out[100]:
0 1 2 3 4
c NaN NaN NaN NaN NaN
a 5.0 7.0 9.0 11.0 13.0
d 15.0 17.0 19.0 21.0 23.0
b 25.0 27.0 29.0 31.0 33.0
b.rolling(3).sum()
Out[101]:
0 1 2 3 4
c NaN NaN NaN NaN NaN
a NaN NaN NaN NaN NaN
d 15.0 18.0 21.0 24.0 27.0
b 30.0 33.0 36.0 39.0 42.0
八摇庙、數(shù)據(jù)的相關(guān)分析
- 正相關(guān)
- 負(fù)相關(guān)
- 不相關(guān)
協(xié)方差
- 協(xié)方差>0,x和y正相關(guān)
- 協(xié)方差<0,x和y負(fù)相關(guān)
- 協(xié)方差=0,x和y不相關(guān)
Pearson相關(guān)系數(shù)(r取值范圍[-1,1])
- 0.8-1.0 極強(qiáng)相關(guān)
- 0.6-0.8 強(qiáng)相關(guān)
- 0.4-0.6 中等程度相關(guān)
- 0.2-0.4 弱相關(guān)
- 0.0-0.2 極弱相關(guān)
相關(guān)分析函數(shù)
適用于Series和DataFrame類型
方法 |
說明 |
.cov() |
計(jì)算協(xié)方差矩陣 |
.corr() |
計(jì)算相關(guān)系數(shù)矩陣旱物,Pearson、Spearman卫袒、Kendall等系數(shù) |
import pandas as pd
hprice = pd.Series([3.04, 22.96, 12.75, 22.6, 12.33], index=['2008','2009', '2010', '2011', '2012'])
m2 = pd.Series([8.18, 18.38, 9.3, 7.82, 6.69], index=['2008','2009', '2010', '2011', '2012'])
hprice.corr(m2)
Out[105]: 0.5237550775533143