python - 如何从 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 的平均值、中位数和众数?无论我如何尝试,我都无法将它们召唤为列表。我环顾四周,还没有找到这样的案例。
将不胜感激。谢谢
解决方案
我想终于明白如何做到这一点了。
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
推荐阅读
- applescript - 如何将任何应用程序的选定文本放入 Safari 到 google(AppleScript)
- c - C语言中的硬币翻转程序
- django-rest-framework - Django Rest Framework:我们如何称呼显示 DRF 中所有 url 的网页
- python - 如何使用颜色名称作为颜色绘制颜色的频率直方图
- r - 在 RStudio 中绘制美国地图上的经纬度点时出错
- javascript - 为什么模运算符在 javscript 的 FOR 循环中的工作方式不同?
- node.js - 如何使用 dynamodb 查询操作模拟/单元测试分页?
- rust - 对于模块路径+类型名称,在宏示例中应该使用哪些片段说明符(元变量类型)?
- ios - 我在 SwiftUI 中找到了两种管理视图的方法,但我不知道有什么区别,也不知道哪种方法更好
- r - 具有泊松分布的对数线性模型的偏移项