Reputation: 9
I have following dictionairy:
total_working_years_dict = dict(df.TotalWorkingYears.value_counts())
total_working_years_dict
{10: 202,
6: 125,
8: 103,
9: 96,
5: 88,
1: 81,
7: 81,
4: 63,
12: 48,
3: 42,
15: 40,
16: 37,
13: 36,
11: 36,
21: 34,
17: 33,
14: 31,
2: 31,
20: 30,
18: 27,
19: 22,
23: 22,
22: 21,
24: 18,
25: 14,
28: 14,
26: 14,
0: 11,
29: 10,
31: 9,
32: 9,
27: 7,
30: 7,
33: 7,
36: 6,
34: 5,
37: 4,
35: 3,
40: 2,
38: 1}
The keys are working years and values are numbers of employees which have such experience. I would like to transform my dictionairy so that total working yeras are given in ranges (0,6), (6,11) etc.
Do you have any idea how to do that ?
Upvotes: 0
Views: 68
Reputation: 261900
Let's start from your dict as a Series:
s = pd.Series(total_working_years_dict)
you can use pandas.cut
to form your groups:
s.index = pd.cut(s.index, bins=range(0,100,6))
output:
(6.0, 12.0] 202
(0.0, 6.0] 125
(6.0, 12.0] 103
(6.0, 12.0] 96
(0.0, 6.0] 88
...
(30.0, 36.0] 3
(36.0, 42.0] 2
(36.0, 42.0] 1
dtype: int64
NB. if you now want to aggregate the counts per group, it would be more efficient to proceed to the pandas.cut
operation before your initial value_counts
. Also, I don't get the point of converting the Series to dict if you need to further process it.
Upvotes: 1