简体   繁体   中英

Creating a new Min() and Max() column based another dataframe

I'm trying to create two columns in a new dataframe base on the Min and the Min from an existing dataframe. When groupby is used, it is giving min and max NAN values

df.groupby('street').min()['sold_price']
df.groupby('street').max()['sold_price']

sample from existing dataframe.

street_name sold_price
A            100,000
A            200,100
B            50,000
B            100,000

new dataframe should be

street_name   min        max
A             100,000    200,000
B             50,000     100,000

It should be

(df.groupby("street_name", as_index=False)
    ["sold_price"].agg(["min","max"])
)

Update : for rename:

(df.groupby("street_name", as_index=False)
    ["sold_price"].agg({'low':'min', 'high':'max'})
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM