简体   繁体   中英

Pandas group values and get their average

So my df looks something like this:

    Model       Price
   1 Series     £42000
   1 Series     £45000
   2 Series     £32000 
   2 Series     £25000

I would like to get the mean price for each model, and map that as a bar chart. I have tried df.groupby(['Model','MSRP']).mean(); however, that does not display the average price for each model, it just groups it looking something like this:

   Model        Price
   1 Series     £42000
                £45000
   2 Series     £32000
                £25000

You could lstrip £ , cast to integer, and then compute the GroupBy.mean and plot:

df.Price.str.lstrip('£').astype(int).groupby(df.Model).mean().plot.barh()

在此处输入图片说明

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