Issue
I have a data frame and i am creating bins with pd.qcut as following:
us_counties['bins'] = pd.qcut(us_counties['economic connectedness'], q=10,precision=2)
The bins are:
us_counties.bins.cat.categories
IntervalIndex([(0.27999999999999997, 0.58], (0.58, 0.67], (0.67, 0.72], (0.72, 0.76], (0.76, 0.81], (0.81, 0.85], (0.85, 0.9], (0.9, 0.97], (0.97, 1.06], (1.06, 1.36]], dtype='interval[float64, right]')
I want to change their format so the first bin is <0.58, the medium ones 0.67-0.72 and the last one >1.06.
I managed to make the format of the medium ones with the following command:
us_counties.bins.cat.categories = [f'{i.left} - {i.right}' for i in us_counties.bins.cat.categories]
How can I change the first and last one, so that I end with bins that look like:
['<0.58','0.58 - 0.67',....,'0.97 - 1.06','>1.06']
Solution
How about something like this?
mybinlabels = [f'{i.left} - {i.right}' for i in us_counties.bins.cat.categories]
mybinlabels[0] = ["<"+str(i.right) for i in [us_counties.bins.cat.categories[0]]]
mybinlabels[-1] = [">"+str(i.left) for i in [us_counties.bins.cat.categories[-1]]]
us_counties.bins.cat.categories = mybinlabels
Answered By - Vincent Rupp
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.