pandas cut multiple columns

I am looking to apply a bin across a number of columns.

a = [1, 2, 9, 1, 5, 3]
b = [9, 8, 7, 8, 9, 1]

c = [a, b]

print(pd.cut(c, 3, labels=False))

which works great and creates:

[[0 0 2 0 1 0]
[2 2 2 2 2 0]]

However, i would like to apply the 'cut' to create a dataframe with number and bin it as below.

   Values  bin
0       1    0
1       2    0
2       9    2
3       1    0
4       5    1
5       3    0


   Values  bin
0       9    2
1       8    2
2       7    2
3       8    2
4       9    2
5       1    0 

This is a simple example of what im looking to do. In reality i 63 separate dataframes and a & b are examples of a column from each dataframe.

1 answer

  • answered 2018-01-14 10:42 cᴏʟᴅsᴘᴇᴇᴅ

    Use zip with a list comp to build a list of dataframes -

    c = [a, b]
    r = pd.cut(c, 3, labels=False)
    
    df_list = [pd.DataFrame({'Values' : v, 'Labels' : l}) for v, l in zip(c, r)]
    

    df_list
    
    [   Labels  Values
     0       0       1
     1       0       2
     2       2       9
     3       0       1
     4       1       5
     5       0       3,    Labels  Values
     0       2       9
     1       2       8
     2       2       7
     3       2       8
     4       2       9
     5       0       1]