Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas cut multiple columns

Tags:

python

pandas

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.

like image 550
ben121 Avatar asked Jan 14 '18 10:01

ben121


1 Answers

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]
like image 59
cs95 Avatar answered Oct 20 '22 14:10

cs95