Home > Mobile >  From pandas interval to a legend-friendly string
From pandas interval to a legend-friendly string

Time:12-21

Pandas.cut returns one or several intervals. I want to use that information to create label-friend strings. My goal is to from iv in the code, get this string: "20-35 %".

iv=pd.Interval(left=0.2, right=0.35)

I've tried this so far, but it doesn't, to me at least, seem like an optimal solution. Further on this information will be used in a plot legend. So I also want to get the categories into the right order in some automated fashion.

iv=pd.Interval(left=0.2, right=0.35)
iv=iv*100
a=str(iv)
b=str(iv).replace('(', '').replace(']', '').replace(',', '-').replace(' ', '')
c=b   ' %'

CodePudding user response:

One could leverage Interval variables and use as starting point

f"{iv.left*100}-{iv.right*100}%"

or (formatted) variations of it maybe:

f"{(iv.left*100):.0f}-{(iv.right*100):.0f}%"
  • Related