gpt4 book ai didi

python - 是否有相当于 dplyr::summarise 的 Pandas ?

转载 作者:太空宇宙 更新时间:2023-11-04 05:30:51 27 4
gpt4 key购买 nike

在R/dplyr中,我可以做到

summarise(iris, max_width=max(Sepal.Width), min_width=min(Sepal.Width))

并得到:

  max_width min_width
1 4.4 2

pandas中有没有类似summarise的东西?我知道 describe(),但我希望结果只包含给定列的给定汇总统计信息,而不是所有列的所有汇总统计信息。在 pandas 中,iris.describe() 给出:

        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

最佳答案

从 0.20 版开始,agg 也可以在 DataFrames 上调用 ( source )。

所以你可以这样做:

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

另见 dplyr summarize equivalent in pandas .不过,那个侧重于 groupby 操作。

关于python - 是否有相当于 dplyr::summarise 的 Pandas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37209908/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com