Pandas库学习笔记(9) Pandas中Series和DataFrame数据类型的相关分析

参考链接: 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学习笔记> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值