0

I got the following DataFrame:

 R System Name 0 0.78 1 Methane - PAH 1 1.00 1 Methane - PAH 2 1.20 1 Methane - PAH 3 1.40 1 Methane - PAH 4 0.90 1 Methane - PAH 5 2.16 1 Methane - PAH 6 1.62 1 Methane - NT 7 1.35 1 Methane - NT 8 1.19 2 Methane - NT 9 1.14 2 Methane - NT 10 0.80 2 Methane - NT 11 1.03 2 Methane - PAH 12 0.89 2 Methane - PAH 13 0.92 2 Methane - PAH 14 1.08 2 Methane - PAH 15 0.86 2 Methane - PAH 16 0.84 2 Methane - PAH 17 0.95 3 Methane - PAH 19 0.97 3 Methane - PAH 20 1.00 3 Methane - NT 21 0.92 3 Methane - NT 22 0.80 3 Methane - PAH 23 0.90 3 Methane - PAH 24 1.00 3 Methane - PAH 25 1.20 3 Methane - NT 26 0.87 3 Methane - NT 27 1.40 3 Methane - PAH 28 0.89 3 Methane - PAH 

I want to sort the Frame according to R but around a particular System :

 R System Name 0 0.78 1 Methane - PAH 1 0.90 1 Methane - PAH 2 1.00 1 Methane - PAH 3 1.20 1 Methane - PAH 4 1.35 1 Methane - NT 5 1.40 1 Methane - PAH 6 1.62 1 Methane - NT 7 0.80 2 Methane - NT 8 0.89 2 Methane - PAH 9 0.92 2 Methane - PAH 10 1.03 2 Methane - PAH 11 1.08 2 Methane - PAH 12 1.14 2 Methane - NT 13 1.19 2 Methane - NT ... 18 0.80 3 Methane - PAH 19 0.87 3 Methane - NT 20 0.90 3 Methane - PAH 21 0.92 3 Methane - NT 22 0.95 3 Methane - PAH 23 0.97 3 Methane - PAH 24 1.00 3 Methane - NT 25 1.00 3 Methane - PAH 26 1.20 3 Methane - NT 28 1.40 3 Methane - PAH 

I know how to sort DataFrame by a column: df = df.sort_values(by = ['R'], ascending=True)

However I do not know how to do a 'partial sort'. I will be grateful for any hints.

3
  • 1
    Where did z suddenly come from? Commented May 3, 2017 at 17:12
  • 9
    Pandas' sort_values() function takes a list. You should be able to use df = df.sort_values(by = ['System', 'R'], ascending=True). Commented May 3, 2017 at 17:15
  • 4
    @SNygard, you can turn your comment into an answer Commented May 3, 2017 at 17:22

1 Answer 1

1

Since version 17.0, pandas has supported sorting by multiple column names. The documentation for the sort_values() function shows that you can pass a list to the by= keyword. The function will return a dataframe sorted by the first column in the list. Then within all matching values in the first column, the dataframe will be sorted by the second column, and so on.

The function call to sort your dataframe would be df = df.sort_values(by = ['System', 'R'], ascending=True).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.