简体   繁体   中英

How to sort value after using groupby and choose the most frequent one?

So Actually there are two question, first one is how I can get this sorted? I tried to add sort_value() following .mean() but it's not working. 第一个问题

The second question is I can use .mean() to get the average value for price after groupby. How I can get the most frequent one? For example I want to get 100 if 100 is the most frequent price in area Allson.

Thank you in advance for the help.

For question 1, df.groupby('neighourhood_cleansed').apply(pd.DataFrame.sort_values, 'price') . After doing the group by, we are sorting on the group_by dataframe object so we use apply to say we do sort on the column price.

For question 2, try df['col'].mode() .

df.groupby('...').apply(pd.DataFrame.mode, 'col_name') will work after a groupby.

For question 1:

df.groupby('neighourhood_cleansed').apply(pd.DataFrame.sort_values, 'price')

groupby + apply +sorting for above

For question 2:

df['col'].mode()

mode for above

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