python - With `pandas.cut()`, how do I get integer bins and avoid getting a negative lowest bound? - Stack Overflow

PHOTO EMBED

Wed Oct 06 2021 10:15:01 GMT+0000 (Coordinated Universal Time)

Saved by @Mukil #python

bins = 7

_, edges = pd.cut(df.value, bins=bins, retbins=True)
labels = [f'({abs(edges[i]):.0f}, {edges[i+1]:.0f}]' for i in range(len(edges)-1)]

df['bin'] = pd.cut(df.value, bins=bins, labels=labels)

#     value         bin
# 1       8     (0, 53]
# 2      16     (0, 53]
# ..    ...         ...
# 45    360  (322, 376]
# 46    368  (322, 376]
content_copyCOPY

https://stackoverflow.com/questions/32552027/with-pandas-cut-how-do-i-get-integer-bins-and-avoid-getting-a-negative-lowe