Python Forum
Pandas segmenting groupby average
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas segmenting groupby average
#1
I have 3 columns in a pandas dataframe, cars bikes and price. I want to find the average price per cars and bikes. The data seems to have decimal values for bikes.

3, 1, 221.90
3, 2.5, 538.00
2, 1.25, 180.00
4, 3.5, 604.00
3, 0.75, 510.00
4, 4.5, 123.06
3, 2.5, 257.50
3, 1.5, 291.85

I use
vehicle.groupby(['cars', 'bikes'])['price'].mean()
to get the mean. This gives me the below columns of cars, bikes and average price.

0, 0.00, 545.20
0, 0.75, 234.06
0, 1.00, 256.26
1, 0.00, 285.76
1, 0.50, 237.54
1, 0.75, 234.52
1, 1.00, 374.11
2, 0.50, 123.97
2, 0.75, 343.24

How can I segment this so all values for bikes <1 is considered 1, all values for bikes <2 is considered 2 etc and then work out the average?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove extra count columns created by pandas groupby spyf8 1 1,889 Feb-10-2021, 09:19 AM
Last Post: Naheed
  Combine groupby() and shift() in pandas rama27 0 2,959 Nov-17-2020, 09:49 PM
Last Post: rama27
  Pandas + Groupby + Filter unique values JosepMaria 1 2,158 Jun-15-2020, 08:15 AM
Last Post: JosepMaria
  python pandas: diff between 2 dates in a groupby bluedragon 0 2,853 Mar-25-2020, 04:18 PM
Last Post: bluedragon
  itertuples, datetime, pandas, groupby, in range karlito 0 1,968 Nov-29-2019, 11:35 AM
Last Post: karlito
  Groupby in pandas with conditional - add and subtract rregorr 2 6,152 Jul-12-2019, 05:17 PM
Last Post: rregorr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020