Skip to content Skip to sidebar Skip to footer

Convert Pandas Cut Operation To A Regular String

I get the foll. output from a pandas cut operation: 0 (0, 20] 1 (0, 20] 2 (0, 20] 3 (0, 20] 4 (0, 20] 5 (0, 20] 6 (0, 20] 7

Solution 1:

Use the labels parameter of pd.cut:

pd.cut(df['some_col'], bins=[0,20,40,60], labels=['0-20', '20-40', '40-60']) 

I don't know what your exact pd.cut command looks like, but the code above should give you a good idea of what to do.

Example usage:

df = pd.DataFrame({'some_col': range(5, 56, 5)})
df['cut'] = pd.cut(df['some_col'], bins=[0,20,40,60], labels=['0-20','20-40','40-60'])

Example output:

    some_col    cut
0          5   0-20
1         10   0-20
2         15   0-20
3         20   0-20
4         25  20-40
5         30  20-40
6         35  20-40
7         40  20-40
8         45  40-60
9         50  40-60
10        55  40-60

Solution 2:

assuming the output was assigned to a variable cut

cut.astype(str)

To remove bracketing

cut.astype(str).str.strip('()[]')

Post a Comment for "Convert Pandas Cut Operation To A Regular String"