python - Pandas sorting dataframe on the basis of other column value -
i have sorted pandas dataframe df
.
i use df = df.sort(col_a,col_b)
col_a col_b 6 7 8 11 b 5 b 10 b 12 c 11 c 13 c 14
but i'd sort df
on basis of col_b
min , max value have in first place col_a
value min col_b
value , @ last place col_a
value max col_b
value :
col_a col_b b 5 b 10 b 12 6 7 8 11 c 11 c 13 c 14
is there fast way make kind of sorting using pandas fucntion?
edit 1:
@primer solution works 2 columns df
. df
col_a col_b col_c 0 6 9 1 7 8 2 8 7 3 11 6 4 b 5 5 5 b 10 4 6 b 12 3 7 c 11 2 8 c 13 1 9 c 14 0
return
valueerror: wrong number of items passed 2, placement implies 1
edit 2
d = {'col_a' : ['a','a','a','a','b','b','c','c','c'], 'col_b' :[6,7,8,11,12,13,11,13,14], 'col_c' :[9,8,7,6,5,4,3,2,1] } df = dataframe(d)
return:
col_a col_b col_c 0 6 9 1 7 8 2 8 7 3 11 6 4 b 12 5 5 b 13 4 6 c 11 3 7 c 13 2 8 c 14 1
@primer whit df
code doesnt'work because returns:
col_a col_b col_c 0 6 9 1 7 8 2 8 7 3 11 6 4 c 11 3 5 c 13 2 6 c 14 1 7 b 12 5 8 b 13 4
i need have
col_a col_b col_c 0 6 9 1 7 8 2 8 7 3 11 6 4 b 12 5 5 b 13 4 6 c 11 3 7 c 13 2 8 c 14 1
because c
group has max(value)=14
,instead code takes max(min)
values
you this:
df['min'] = df.groupby('col_a')['col_b'].transform(lambda x: x.min()) df = df.sort(['min', 'col_a', 'col_b']).reset_index(drop=true).drop('min', 1) df
which yields:
col_a col_b 0 b 5 1 b 10 2 b 12 3 6 4 7 5 8 6 11 7 c 11 8 c 13 9 c 14
edit:
i have fixed code above make sure transform
used on series , not on dataframe (thus avoiding error).
works me returning:
col_a col_b col_c 0 b 5 5 1 b 10 4 2 b 12 3 3 6 9 4 7 8 5 8 7 6 11 6 7 c 11 2 8 c 13 1 9 c 14 0
i guess turn function apply on dataframe inplace.
Comments
Post a Comment