基于另一个数据框创建新的Min()和Max()列

时间:2020-03-06 03:47:20

标签: python pandas

我正在尝试基于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

1 个答案:

答案 0 :(得分:0)

应该是

(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'})
)