Sort dataframe in decreasing order

Obviously, you should know what I want but the trick is to sort according to the group.


Take a look at the image and you should see two groups: "Best departing flights" and "Best returning flights". The dataframe should be sorted by total column in decreasing order
What I want the output to look like is:
Best departing flights

Price Total

49 49

55 55

129 258

Best returning flights

60 60

212 424

242 484

I tried posting the data using dput, but I couldn't because I made a function

                  Airline Date_of_Journey   Source Destination           Route Dep_Time Arrival_Time
1  Best departing flights                                                                           
2               Air India      03/01/2019 Banglore   New Delhi BLR → BOM → DEL 06:45 AM     12:15 PM
3                  IndiGo      03/06/2019  Kolkata    Banglore       CCU → BLR 09:25 PM     12:05 AM
4             Jet Airways      03/01/2019 Banglore   New Delhi BLR → BOM → DEL 08:55 AM     09:20 PM
5  Best returning flights                                                                           
51               SpiceJet      03/06/2019  Kolkata    Banglore       CCU → BLR 05:15 PM     07:45 PM
6                SpiceJet      03/06/2019  Kolkata    Banglore       CCU → BLR 09:00 AM     11:30 AM
7                 Vistara      03/01/2019 Banglore   New Delhi       BLR → DEL 09:10 PM     12:05 AM
   Duration Total_Stops Additional_Info Price Total
1                                                  
2    5h 30m      1 stop         No info    49    49
3    2h 40m    non-stop         No info   129   258
4   12h 25m      1 stop  1 Long layover    55    55
5                                                  
51   2h 30m    non-stop         No info   242   484
6    2h 30m    non-stop         No info   212   424
7    2h 55m    non-stop         No info    60    60

In addition, I just noticed that the order of the Date_of_Journey is incorrect. This is how it should look:

Best departing flights

03/01/2019

03/01/2019

03/01/2019

Best returning flights

03/06/2019

03/06/2019

03/06/2019

Would group_by from the dplyr package be a better option?

The problem here is that you have 2 dataframes, not 1. Two solutions to make it easy to work with:

  • separate the "best departing flights" and the "best returning flights" dataframes so that you can work with each separately and combine them as you wish, or
  • have a single dataframe that combines the two, with an additional column that just contains "departing" or "returning" for each row.

Which format is easier to create depends on your data source. How do you obtain the input? Is it a single big Excel file? Do they come from separate SQL requests?

Once you have them in one of those formats, it's easy to convert to the other, but with the data as copy-pasted here it's a bit of an additional effort.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.