有一个 pandas 相当于dplyr ::总结? [英] Is there a pandas equivalent of dplyr::summarise?
本文介绍了有一个 pandas 相当于dplyr ::总结?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
总结(iris,max_width = max(Sepal.Width),min_width = min(Sepal.Width))
并获取:
max_width min_width
1 4.4 2
有没有类似于在大熊猫中总结
的东西?我知道 describe()
,但我希望结果只包含给定列的给定摘要统计信息,而不是所有列的所有摘要统计信息。在大熊猫中, iris.describe()
给出:
sepal_length sepal_width petal_length petal_width
计数150.000000 150.000000 150.000000 150.000000
平均5.843333 3.057333 3.758000 1.199333
std 0.828066 0.435866 1.765298 0.762238
最小4.300000 2.000000 1.000000 0.100000
25%5.100000 2.800000 1.600000 0.300000
50%5.800000 3.000000 4.350000 1.300000
75%6.400000 3.300000 5.100000 1.800000
最大7.900000 4.400000 6.900000 2.500000
解决方案
从版本0.20起,可以在DataFrames上调用 agg
(来源)。
所以你可以这样做:
iris.agg({'sep al_width':'min','petal_width':'max'})
petal_width 2.5
sepal_width 2.0
dtype:float64
iris.agg ({'sepal_width':['min','median'],'sepal_length':['min','mean']})
sepal_length sepal_width
平均值5.843333 NaN
中位数NaN 3.0
最小4.300000 2.0
另请参阅 dplyr总结了大熊猫的等价物。那个专注于groupby操作。
In R/dplyr, I can do
summarise(iris, max_width=max(Sepal.Width), min_width=min(Sepal.Width))
and get:
max_width min_width
1 4.4 2
Is there something similar to summarise
in pandas? I know describe()
, but I would like the result to only contain a given summary statistic for a given column, not all summary statistics for all columns. In pandas, iris.describe()
gives:
sepal_length sepal_width petal_length petal_width
count 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.057333 3.758000 1.199333
std 0.828066 0.435866 1.765298 0.762238
min 4.300000 2.000000 1.000000 0.100000
25% 5.100000 2.800000 1.600000 0.300000
50% 5.800000 3.000000 4.350000 1.300000
75% 6.400000 3.300000 5.100000 1.800000
max 7.900000 4.400000 6.900000 2.500000
解决方案
As of version 0.20, agg
can be called on DataFrames too (source).
So you can do things like:
iris.agg({'sepal_width': 'min', 'petal_width': 'max'})
petal_width 2.5
sepal_width 2.0
dtype: float64
iris.agg({'sepal_width': ['min', 'median'], 'sepal_length': ['min', 'mean']})
sepal_length sepal_width
mean 5.843333 NaN
median NaN 3.0
min 4.300000 2.0
Also see dplyr summarize equivalent in pandas. That one focuses on groupby operations though.
这篇关于有一个 pandas 相当于dplyr ::总结?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文