I have a pandas dataframe like this:
df =
userid item_id label score
1 [1, 2, 3] [0, 0, 1] [0.2, 0.3, 0.5]
... ... ...
item_id
, label
and score
are the same sized list columns of df (not limited to size 3). I want to sort score
column in descending order and label
and item_id
also should be sorted in reference with the new order of score
column.
Expected Output
df =
userid. item_id. truth score
1 [3, 2, 1] [1, 0, 0] [0.5, 0.3, 0.2]
Is it possible to do this in Pandas?
How I formed this dataframe:
I had this data frame originally:
df_original =
userid item_id label score
1 1 0 0.2
1 2 0 0.3
1 3 1 0.5
I called:
df = df_original.groupby('user_id').agg(list)
If there is a way achieving the Expected Output
from df_original
, then also it is fine.
try sort_values
and groupby
to convert to list again
(df_original.sort_values(['userid', 'score'], ascending=[True,False])
.groupby('userid').agg(list)
.reset_index()
)
As @Ben.T warned, it is possible to make it simpler
(df_original.sort_values('score', ascending=False)
.groupby('userid').agg(list)
.reset_index()
)