Sorting only specific subset of rows in pandas dataframe

Question:

I spent some time trying to figure out a solution to but haven’t been able to figure a simple and clean solution to my problem. Basically I have the following dataframe:

Plane Parts Quantity is_plane
G6_32 FAB 1 True
G6_32 KIT 2 True
Item D 2 False
Item C 4 False
Item A 5 False
G6_32 SITE 5 True
G6_32 SPACE 6 True
Item C 2 False
Item A 1 False
Item F 2 False

I need to sort only the subset of rows which have is_plane == False. So at the end my final result would look like:

Plane Parts Quantity is_plane
G6_32 FAB 1 True
G6_32 KIT 2 True
Item A 5 False
Item C 4 False
Item D 2 False
G6_32 SITE 5 True
G6_32 SPACE 6 True
Item A 1 False
Item C 2 False
Item F 2 False

Notice that the rows which is_plane == True are not supposed to be sorted and kept the original position. Any idea on how to achieve it?

Asked By: Michell Germano

||

Answers:

make grouper for grouping

grouper = df['is_plane'].ne(df['is_plane'].shift(1)).cumsum()

grouper:

0    1
1    1
2    2
3    2
4    2
5    3
6    3
7    4
8    4
9    4
Name: is_plane, dtype: int32

use groupby by grouper

group that its ‘Plane Parts’ is all False, sort_values by Plane Parts.

df.groupby(grouper).apply(lambda x: x.sort_values('Plane Parts') if x['is_plane'].sum() == 0 else x).droplevel(0)

output:

    Plane Parts Quantity    is_plane
0   G6_32 FAB   1           True
1   G6_32 KIT   2           True
4   Item A      5           False
3   Item C      4           False
2   Item D      2           False
5   G6_32 SITE  5           True
6   G6_32 SPACE 6           True
8   Item A      1           False
7   Item C      2           False
9   Item F      2           False
Answered By: Panda Kim