Python Forum

Full Version: Pandas segmenting groupby average
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?