Group Column Using Difference And Sorting One Column By Other Column In Pandas
Hi i'm trying to group a column by values that are closer each other, and then sorting another column by the values of other, example: column1 column2 column3 322 16 a 326 11
Solution 1:
In this case , we need create another help key after sort_values
with cumsum
and diff
s=df.sort_values(['column1'])
s['New']=s.column1.diff().gt(3).cumsum()
s=s.sort_values(['New','column2'])
s.groupby('New').agg({'column1':'first','column3':','.join})
column1 column3
New
0 326 b,f,a,g,e,c,d
1 496 k,h,j,i
Post a Comment for "Group Column Using Difference And Sorting One Column By Other Column In Pandas"