首页 > 解决方案 > 如何从 Python 3 中的唯一值中获取均值、中值和众数

问题描述

你能帮我按照标题中所说的去做吗?我最近才开始学习python。

我有一个 SalePrice 和 MSZoning 数据集。

    MSZoning    SalePrice
0   RL  208500
1   RL  181500
2   RL  223500
3   RL  140000
4   RL  250000
... ... ...
1455    RL  175000
1456    RL  210000
1457    RL  266500
1458    RL  142125
1459    RL  147500

house = df[['MSZoning', 'SalePrice']]

MSZoning具有RF、RL、RM、C(全部)、FV、RH等多重独特性。问题是,如何获得在另一列中有 RH、RF 和 RL 标签的 SalePrice 的平均值、中位数和众数?无论我如何尝试,我都无法将它们召唤为列表。我环顾四周,还没有找到这样的案例。

将不胜感激。谢谢

标签: pythonpython-3.xpandasmultiple-columns

解决方案


我想终于明白如何做到这一点了。

df['column I want to use its values'].loc['column that I use as pivot for filter' == 'category used']

例如,使用 Daweo 的数据集,我想列出 A 的值并将其放入变量中:

A = df['price'].loc['zone' == 'A']

获得 A 后,我可以这样做来获得均值和中位数:

A.mean() #getting mean
np.median(A) #getting median

推荐阅读