Pandas df.describe() ,是否可以在不转置的情况下逐行进行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34085081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas df.describe() , is it possible to do it by row without transposing?
提问by alec_djinn
Pandas df.describe()is a very useful method to have an overview of your df. However, it describes by columns and I would like to have an overview of the rows instead. Is there any way to make it work "by_row" without transposing the df?
Pandas df.describe()是一种非常有用的方法,可以概览您的 df。但是,它按列进行描述,而我想对行进行概述。有没有办法让它在不转置 df 的情况下“by_row”工作?
回答by EdChum
Use apply
and pass axis=1
to call describe
row-wise:
使用apply
和传递axis=1
来按describe
行调用:
In [274]:
df = pd.DataFrame(np.random.randn(4,5))
df
Out[274]:
0 1 2 3 4
0 0.651863 0.738034 -0.477668 -0.561699 0.047500
1 -1.565093 -0.671551 0.537272 -0.956520 0.301156
2 -0.951549 2.177592 0.059961 -1.631530 -0.620173
3 0.277796 0.169365 1.657189 0.713522 1.649386
In [276]:
df.apply(pd.DataFrame.describe, axis=1)
Out[276]:
count mean std min 25% 50% 75% max
0 5 0.079606 0.609069 -0.561699 -0.477668 0.047500 0.651863 0.738034
1 5 -0.470947 0.878326 -1.565093 -0.956520 -0.671551 0.301156 0.537272
2 5 -0.193140 1.458676 -1.631530 -0.951549 -0.620173 0.059961 2.177592
3 5 0.893451 0.722917 0.169365 0.277796 0.713522 1.649386 1.657189
回答by Igor Fobia
EdChum answer did not work for me, because with the apply command all the rows are Series; by usingpd.Series.describe
it worked
EdChum 的回答对我不起作用,因为使用 apply 命令,所有行都是系列;通过使用pd.Series.describe
它工作
df.apply(pd.Series.describe, axis=1)