import pandas as pd import numpy as np #Series结构 s = pd.Series([1,2,3,4,5,4]) print (s.pct_change()) #DataFrame df = pd.DataFrame(np.random.randn(5, 2)) print(df.pct_change())输出结果:
0 NaN 1 1.000000 2 0.500000 3 0.333333 4 0.250000 5 -0.200000 dtype: float64 0 1 0 NaN NaN 1 74.779242 0.624260 2 -0.353652 -1.104352 3 -2.422813 -13.994103 4 -3.828316 -1.853092默认情况下,pct_change() 对列进行操作,如果想要操作行,则需要传递参数 axis=1 参数。示例如下:
import pandas as pd import numpy as np #DataFrame df = pd.DataFrame(np.random.randn(3, 2)) print(df.pct_change(axis=1))输出结果:
0 1 0 NaN 3.035670 1 NaN -0.318259 2 NaN 0.227580
cov
方法用来计算 Series 对象之间的协方差。同时,该方法也会将缺失值(NAN )自动排除。import pandas as pd import numpy as np s1 = pd.Series(np.random.randn(10)) s2 = pd.Series(np.random.randn(10)) print (s1.cov(s2))输出结果:
0.20789380904226645
当应用于 DataFrame 时,协方差(cov)将计算所有列之间的协方差。import pandas as pd import numpy as np frame = pd.DataFrame(np.random.randn(10, 5), columns=['a', 'b', 'c', 'd', 'e']) #计算a与b之间的协方差值 print (frame['a'].cov(frame['b'])) #计算所有数列的协方差值 print (frame.cov())输出结果:
-0.37822395480394827 a b c d e a 1.643529 -0.378224 0.181642 0.049969 -0.113700 b -0.378224 1.561760 -0.054868 0.144664 -0.231134 c 0.181642 -0.054868 0.628367 -0.125703 0.324442 d 0.049969 0.144664 -0.125703 0.480301 -0.388879 e -0.113700 -0.231134 0.324442 -0.388879 0.848377
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(10, 5), columns=['a', 'b', 'c', 'd', 'e']) print (df['b'].corr(frame['c'])) print (df.corr())输出结果:
0.5540831507407936 a b c d e a 1.000000 -0.500903 -0.058497 -0.767226 0.218416 b -0.500903 1.000000 -0.091239 0.805388 -0.020172 c -0.058497 -0.091239 1.000000 0.115905 0.083969 d -0.767226 0.805388 0.115905 1.000000 0.015028 e 0.218416 -0.020172 0.083969 0.015028 1.000000注意:如果 DataFrame 存在非数值(NAN),该方法会自动将其删除。
import pandas as pd import numpy as np #返回5个随机值,然后使用rank对其排名 s = pd.Series(np.random.randn(5), index=list('abcde')) s['d'] = s['b'] print(s) #a/b排名分别为2和3,其平均排名为2.5 print(s.rank())输出结果:
a -0.689585 b -0.545871 c 0.148264 d -0.545871 e -0.205043 dtype: float64 排名后输出: a 1.0 b 2.5 c 5.0 d 2.5 e 4.0 dtype: float64
ascening
参数, 默认为 True 代表升序;如果为 False,则表示降序排名(将较大的数值分配给较小的排名)。import pandas as pd import numpy as np a = pd.DataFrame(np.arange(12).reshape(3,4),columns = list("abdc")) a =a.sort_index(axis=1,ascending=False) a.iloc[[1,1],[1,2]] = 6 #按行排名,将相同数值设置为所在行数值的最大排名 print(a.rank(axis=1,method="max"))输出结果:
d c b a 0 3.0 4.0 2.0 1.0 1 4.0 4.0 4.0 1.0 2 3.0 4.0 2.0 1.0与 method="min"进行对比,如下所示:
import pandas as pd import numpy as np a = pd.DataFrame(np.arange(12).reshape(3,4),columns = list("abdc")) a =a.sort_index(axis=1,ascending=False) a.iloc[[1,1],[1,2]] = 6 #按行排名,将相同数值设置为所在行数值的最小排名 print(a.rank(axis=1,method="min"))输出结果:
d c b a 0 3.0 4.0 2.0 1.0 1 2.0 2.0 2.0 1.0 2 3.0 4.0 2.0 1.0
本文链接:http://task.lmcjl.com/news/17273.html