python – 如何计算数据帧行的标准差?

python – 如何计算数据帧行的标准差?,第1张

概述df: name group S1 S2 S3 A mn 1 2 8 B mn 4 3 5 C kl 5 8 2 D kl 6 5 5 E fh 7 @H_301_6@
df:  name   group   S1   S2  S3        A      mn      1    2   8         B      mn      4    3   5        C      kl      5    8   2        D      kl      6    5   5         E      fh      7    1   3         output: std (S1,S2,S3)3.78130.573.05

这是为了获取列的std:

numpy.std(df['A'])

我想对行做同样的事情

解决方法 您可以使用 DataFrame.std,它省略非数字列:

print (df.std())S1    2.302173S2    2.774887S3    2.302173dtype: float64

如果需要std列:

print (df.std(axis=1))0    3.7859391    1.0000002    3.0000003    0.5773504    3.055050dtype: float64

如果需要只选择一些数字列,请使用子集:

print (df[['S1','S2']].std())S1    2.302173S2    2.774887dtype: float64

默认情况下参数ddof(Delta degrees of Freedom)与numpy.std有所不同:

> pandas默认为ddof = 1
> numpy默认为ddof = 0

所以有不同的输出:

#ddof=1print (df.std(axis=1))0    3.7859391    1.0000002    3.0000003    0.5773504    3.055050dtype: float64#ddof=0print (np.std(df,axis=1))0    3.0912061    0.8164972    2.4494903    0.4714054    2.494438dtype: float64

但你可以很容易地改变它:

#same output as pandas functionprint (np.std(df,ddof=1,axis=1))0    3.7859391    1.0000002    3.0000003    0.5773504    3.055050dtype: float64#same output as numpy functionprint (df.std(ddof=0,axis=1))0    3.0912061    0.8164972    2.4494903    0.4714054    2.494438dtype: float64
总结

以上是内存溢出为你收集整理的python – 如何计算数据帧行的标准差?全部内容,希望文章能够帮你解决python – 如何计算数据帧行的标准差?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1193686.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存