简体   繁体   中英

Sort a column in Pandas dataframe after groupby operation

I have the following Pandas dataframe:

Fruit   Color   Version
Apple   Green   1.1
Lemon   Green   1.4a
Lemon   Yellow  5
Banana  Black   6.8.a
Banana  Yellow  6.8.a
Lemon   Blue    6.7.a

I would like to group by "Color" column and sort by "Version" column in descending order

The desired output it:

Fruit   Color   Version
Lemon   Green   1.4a
Apple   Green   1.1
Banana  Yellow  6.8.a
Lemon   Yellow  5
Banana  Black   6.8.a
Lemon   Blue    6.7.a

I have tried using LooseVersion library for the version sorting but I can't make it work with 'groupby' method and sort the version inside of each color "chunk"

For the desired output, you don't need to group by but sort for the two columns as follows:


import pandas as pd

df = pd.DataFrame({
    'Fruit': ['Apple', 'Lemon', 'Lemon', 'Banana', 'Banana', 'Lemon'],
    'Color': ['Green', 'Green', 'Yellow', 'Black', 'Yellow', 'Blue'],
    'Version': ['1.1', '1.4a', '5', '6.8.a', '6.8.a', '6.7.a'],
})

print(df)
"""This is your original dataframe
    Fruit   Color Version
0   Apple   Green     1.1
1   Lemon   Green    1.4a
2   Lemon  Yellow       5
3  Banana   Black   6.8.a
4  Banana  Yellow   6.8.a
5   Lemon    Blue   6.7.a
"""

df = df.sort_values(by=['Color', 'Version'], ascending=False)

print(df)
"""This is the sorted dataframe as you want
    Fruit   Color Version
4  Banana  Yellow   6.8.a
2   Lemon  Yellow       5
1   Lemon   Green    1.4a
0   Apple   Green     1.1
5   Lemon    Blue   6.7.a
3  Banana   Black   6.8.a
"""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM