简体   繁体   中英

How to binned filtered pandas data?

All,

The head of my dataset looks like following.I filtered my "Age" and "Absenteeism time in hours" column and calculated the Average of hours. Now I would like to bin based on Age column. How can I perform this ? I would like to bin Age as age31-33,age 34-36,Age 37-39

{'Age': {0: 33, 2: 38, 3: 39, 4: 33, 5: 38}, 'BMI': {0: 30, 2: 31, 3: 24, 4: 30, 5: 31}, 'Social_drinker': {0: 1, 2: 1, 3: 1, 4: 1, 5: 1}, 'Social_smoker': {0: 0, 2: 0, 3: 1, 4: 0, 5: 0}, 'Son': {0: 2, 2: 0, 3: 2, 4: 2, 5: 0}, 'Day_of_the_week': {0: 3, 2: 4, 3: 5, 4: 5, 5: 6}, 'Month_of_absence': {0: 7, 2: 7, 3: 7, 4: 7, 5: 7}, 'Seasons': {0: 1, 2: 1, 3: 1, 4: 1, 5: 1}, 'Service_time': {0: 13, 2: 18, 3: 14, 4: 13, 5: 18}, 'Absenteeism_time_in_hours': {0: 4, 2: 2, 3: 4, 4: 2, 5: 2}}

My code for filtering:

filter= df.filter(['Age','Absenteeism_time_in_hours'], axis=1) 
group=filter.groupby('Age').mean()[['Absenteeism_time_in_hours']] 

I am newbie to python if you can provide explanation that will be great!

You are looking for the function cut . It can be used as follows on your data:

group.groupby(pd.cut(group.index, [31, 33, 36, 39])).mean().fillna(0)

Which results in

          Absenteeism_time_in_hours
(31, 33]                          3
(33, 36]                          0
(36, 39]                          3

As you can see you specify the edges of the bins and it handles the rest for you.

I strongly recommend that you don't bin the grouped results, though, as you get the wrong answer that way. You should rather work with the original data so that you can properly calculate the mean of the individuals in each bin group.

Note the difference in the last age group when you do

(df.groupby(pd.cut(df.Age, [31, 33, 36, 39]))
 .Absenteeism_time_in_hours.mean().fillna(0))

Result:

Age
(31, 33]    3.000000
(33, 36]    0.000000
(36, 39]    2.666667

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