参考链接: Python数据分析与展示
参考链接: Pandas官网
参考链接: User Guide
参考链接: Getting started tutorials
相关分析函数:
适用于Series和DataFrame类型
方法 | 说明 |
---|---|
.cov() | 计算协方差矩阵 |
.corr() | 计算相关系数矩阵, Pearson、Spearman、Kendall等系数 |
实验演示:
Microsoft Windows [版本 10.0.18363.1198]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> ls_year = ['2008','2009','2010','2011','2012']
>>> ls_price = [3.04, 22.93, 12.75, 22.6, 12.33]
>>> ls_m2 = [8.18, 18.38, 9.13, 7.82, 6.69]
>>> hprice = pd.Series(ls_price,index=ls_year)
>>> m2 = pd.Series(ls_m2,index=ls_year)
>>> hprice.corr(m2)
0.5239439145220387
>>> m2.corr(hprice)
0.5239439145220387
>>> plt.plot(ls_year, ls_price, ls_year, ls_m2)
[<matplotlib.lines.Line2D object at 0x0000028E224E32C8>, <matplotlib.lines.Line2D object at 0x0000028E29758B08>]
>>> plt.show()
绘图结果截图:
代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
ls_year = ['2008','2009','2010','2011','2012']
ls_price = [3.04, 22.93, 12.75, 22.6, 12.33]
ls_m2 = [8.18, 18.38, 9.13, 7.82, 6.69]
hprice = pd.Series(ls_price,index=ls_year)
m2 = pd.Series(ls_m2,index=ls_year)
print(hprice.corr(m2))
print(m2.corr(hprice))
plt.plot(ls_year, ls_price,'o-', ls_year, ls_m2,'o-')
plt.show()
print(hprice.corr(m2))
print(m2.corr(hprice))
绘图结果:
控制台输出结果:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
尝试新的跨平台 PowerShell https://aka.ms/pscore6
PS C:\Users\chenxuqi\Desktop\pandas学习笔记> & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.11.371526539\pythonFiles\lib\python\debugpy\launcher' '60636' '--' 'c:\Users\chenxuqi\Desktop\pandas学习笔记\test.py'
0.5239439145220387
0.5239439145220387
0.5239439145220387
0.5239439145220387
PS C:\Users\chenxuqi\Desktop\pandas学习笔记>