python - 基于另一个数据框创建一个新的 Min() 和 Max() 列
问题描述
我正在尝试根据现有数据框中的 Min 和 Min 在新数据框中创建两列。当使用 groupby 时,它给出最小和最大 NAN 值
df.groupby('street').min()['sold_price']
df.groupby('street').max()['sold_price']
来自现有数据框的样本。
street_name sold_price
A 100,000
A 200,100
B 50,000
B 100,000
新的数据框应该是
street_name min max
A 100,000 200,000
B 50,000 100,000
解决方案
它应该是
(df.groupby("street_name", as_index=False)
["sold_price"].agg(["min","max"])
)
更新:重命名:
(df.groupby("street_name", as_index=False)
["sold_price"].agg({'low':'min', 'high':'max'})
)
推荐阅读
- batch-file - 通过批处理文件 (.bat) 编译 Inno Setup 项目
- javascript - Javascript提示保存到c#中的变量,单击相同的按钮
- php - 我的 wordpress 简码不起作用并输出“未知”
- sorting - Woocommerce / 按价格对相关产品进行排序
- python - 当我尝试浏览 .xslx 文件时,会打开一个新的 Excel 文档
- c# - 如何从一个txt文件制作一个水果数组,每个水果在C#中有四个属性
- c# - 如何将图像插入 SQL Server?
- php - 如何在 PhpStorm 包含路径中包含部分 PHAR
- php - 从两个数组创建单个变量
- r - r tensorflow ValueError:列 dtype 和 SparseTensors dtype 必须兼容