1、输出百分比变化以及前后指定的行数
a = np.arange(1,13).reshape(6,2) data = DataFrame(a) #计算列的百分比变化,如果想计算行设置axis=1 print(data.pct_change()) ''' 0 1 0 NaN NaN 1 2.000000 1.000000 2 0.666667 0.500000 3 0.400000 0.333333 4 0.285714 0.250000 5 0.222222 0.200000 ''' #输出前五行,默认是5,可以通过设置n参数来设置输出的行数 print(data.head()) ''' 0 1 0 1 2 1 3 4 2 5 6 3 7 8 4 9 10 ''' #输出最后五行 print(data.tail()) ''' 0 1 1 3 4 2 5 6 3 7 8 4 9 10 5 11 12 '''
2、计算DataFrame列与列的相关系数和协方差
a = np.arange(1,10).reshape(3,3) data = DataFrame(a,index=["a","b","c"],columns=["one","two","three"]) print(data) ''' one two three a 1 2 3 b 4 5 6 c 7 8 9 ''' #计算第一列和第二列的相关系数 print(data.one.corr(data.two)) #1.0 #返回一个相关系数矩阵 print(data.corr()) ''' one two three one 1.0 1.0 1.0 two 1.0 1.0 1.0 three 1.0 1.0 1.0 ''' #计算第一列和第二列的协方差 print(data.one.cov(data.two)) #9.0 #返回一个协方差矩阵 print(data.cov()) ''' one two three one 9.0 9.0 9.0 two 9.0 9.0 9.0 three 9.0 9.0 9.0 '''
3、计算DataFrame与列或者Series的相关系数
a = np.arange(1,10).reshape(3,3) data = DataFrame(a,index=["a","b","c"],columns=["one","two","three"]) print(data) ''' one two three a 1 2 3 b 4 5 6 c 7 8 9 ''' #计算data与第三列的相关系数 print(data.corrwith(data.three)) ''' one 1.0 two 1.0 three 1.0 ''' #计算data与Series的相关系数 #在定义Series的时候,索引一定要去DataFrame的索引一样 s = Series([5,3,1],index=["a","b","c"]) print(data.corrwith(s)) ''' one -1.0 two -1.0 three -1.0 '''
注意:在使用DataFrame或Series在计算相关系数或者协方差的时候,都会计算索引重叠的、非NA的、按照索引对齐原则,对于无法对齐的索引会使用NA值进行填充。在使用DataFrame与指定的行或列或Series计算协方差和相关系数的时候,默认都是与DataFrame的列进行计算,如果想要计算行,设置axis参数为1即可。
以上这篇pandas的相关系数与协方差实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年10月07日
2024年10月07日
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]