Home > Enterprise >  group column values with difference of 3(say) digit in python
group column values with difference of 3(say) digit in python

Time:12-03

I am new in python, problem statement is like we have below data as dataframe

df = pd.DataFrame({'Diff':[1,1,2,3,4,4,5,6,7,7,8,9,9,10], 'value':[x,x,y,x,x,x,y,x,z,x,x,y,y,z]})
Diff   value
1       x
1       x
2       y
3       x
4       x
4       x
5       y
6       x
7       z
7       x
8       x
9       y
9       y
10      z

we need to group diff column with diff of 3 (let's say), like 0-3,3-6,6-9,>9, and value should be count

Expected output is like

Diff   x   y   z
0-3    2   1
3-6    3   1
6-9    3       1
>=9        2   1

CodePudding user response:

Example

example code is wrong. someone who want exercise, use following code

df = pd.DataFrame({'Diff':[1,1,2,3,4,4,5,6,7,7,8,9,9,10], 
                   'value':'x,x,y,x,x,x,y,x,z,x,x,y,y,z'.split(',')})

Code

labels = ['0-3', '3-6', '6-9', '>=9']
grouper = pd.cut(df['Diff'], bins=[0, 3, 6, 9, float('inf')], right=False, labels=labels)
pd.crosstab(grouper, df['value'])

output:

value   x   y   z
Diff            
0-3     2   1   0
3-6     3   1   0
6-9     3   0   1
>=9     0   2   1
  • Related