1

I have a dataframe as below:

col1=[1,1,1,2,2,2,3,3,3]
col2=['a','b','c','d','e','f','g','h','i']
col3=[1,2,3,2,3,1,3,1,2]
d={
    "col1":col1,
    "col2":col2,
    "col3":col3
}
dummy= pd.DataFrame(d)

So, the dataframe looks like below:

dummy data

I want to group all the values according to col1 and get values of col2 according to the sorting(decreasing ord) of col3, i.e, I want my end result as: col2= [c,b,a,e,d,f,g,i,h] I have tried the below which resits in col2 sorted in ascending order:

res=dummy.groupby(['col1','col3'])['col2'].apply(sorted).reset_index()

But the above results in [[a],[b],[c]....]]. I don't want each element to be a list in itself. How do I reverse the order? Any help would be greatly appreciated. Thank you.

2 Answers 2

2

There's no need to use groupby here, a simple sort_values on the two columns will suffice:

dummy.sort_values(['col1', 'col3'], ascending=[True, False])

   col1 col2  col3
2     1    c     3
1     1    b     2
0     1    a     1
4     2    e     3
3     2    d     2
5     2    f     1
6     3    g     3
8     3    i     2
7     3    h     1

The order for "col2" is correct, you just need to return it as a list now:

col2_list = (dummy.sort_values(['col1', 'col3'], ascending=[True, False])
                  .get('col2')
                  .tolist())

col2_list
# ['c', 'b', 'a', 'e', 'd', 'f', 'g', 'i', 'h']

In response to a request in the comments:

now I want to combine these col2 values with col1 values, can I directly fetch col1 from dummy df and sorted col2 to create a new dataframe?

The output should look like (eg): 1 [c,b,a] 2 [e,d,f] ...

Here we can build on the previous solution with Groupby.agg to listify the data:

(dummy.sort_values(['col1', 'col3'], ascending=[True, False])
      .groupby('col1', sort=False)['col2']
      .agg(list)
      .reset_index())

   col1       col2
0     1  [c, b, a]
1     2  [e, d, f]
2     3  [g, i, h]
3
  • Thank you so much for your help. I just had one query, after getting the sorted values of col2, can I directly associate these values with col1? Meaning, now I want to combine these col2 values with col1 values, can I directly fetch col1 from dummy df and sorted col2 to create a new dataframe?
    – sra7687
    Dec 24, 2020 at 5:29
  • @Aditya I don't follow, sorry. Do you mean you want to get the col1 values that correspond to sorted col2 values as well? What should the output look like, list of tuples?
    – cs95
    Dec 24, 2020 at 7:00
  • @ca95 The output should look like (eg): 1 [c,b,a] 2 [e,d,f] ...
    – sra7687
    Dec 24, 2020 at 7:03
1

Try:

df.groupby(['col1'])[['col2','col3']].apply(lambda x: x.sort_values('col3',ascending=False)).reset_index(drop=True)['col2']

Prints:

0    c
1    b
2    a
3    e
4    d
5    f
6    g
7    i
8    h

To print as a list use series.tolist()

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.