-1

I have a dataframe of 500 rows. I want to sort it by the column "ESG Combined Score 2011" and did that (see below). The problem is, that it sorts only by this last column and doesn't change the corresponding companies and market cap. That means that the first 2 columns doesn't change based on the last column (All columns should be sticked together). I just want to sort the companies by it's ESG Combined Score. What should I do here?

Many Thanks in advance

My code for this operation and the corresponding output

Before, I extract these three columns out of my dataset (which contains 50 columns and 500 rows):

df = pd.DataFrame(data=dataset)    
 df1 = df[['Company Common Name', 'Company Market Capitalization', 'ESG Combined Score 2011']]

The output before I sort the last column ("ESG Combined Score")

df1
    Company Common Name Company Market Capitalization   ESG Combined Score 2011
0   SSR Mining Inc  3.129135e+09    32.817325
1   Fluor Corp  3.958424e+09    69.467729
2   CBRE Group Inc  2.229251e+10    59.632423
3   Assurant Inc    8.078239e+09    46.492803
4   CME Group Inc   6.269954e+10    42.469682
5   Peabody Energy Corp 3.842130e+09    73.374671

Code I used for sorting:

sorted_2011 = df1.sort_values(by= ['ESG Combined Score 2011'], ascending=False)

Output after that:

    Company Common Name Company Market Capitalization   ESG Combined Score 2011
0   SSR Mining Inc  3.129135e+09    88.881182
1   Fluor Corp  3.958424e+09    85.249202
2   CBRE Group Inc  2.229251e+10    84.357820
3   Assurant Inc    8.078239e+09    84.191950
4   CME Group Inc   6.269954e+10    82.592251
5   Peabody Energy Corp 3.842130e+09    81.509097

As you can see, the last column is sorted which is fine. BUT the first 2 columns are the same as before (which should not be the case) In fact the 88.88 score corresponds to the Company "Texas Instruments Inc." and not "SSR Mining" like in the example above.

What is wrong here?

  • 3
    `The problem is, that it sorts only by this last column and doesn't change the corresponding companies and market cap` - Are you sure? I think `sort_values` working well here - from ouput in pic. Can you add sample data before `sort_values` and expected ouput? – jezrael Oct 17 '22 at 10:34
  • 2
    Can you include a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? In plain text please, not as an image. – RJ Adriaansen Oct 17 '22 at 10:35
  • Please, provide a [reproducible pandas example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – alec_djinn Oct 17 '22 at 12:07

1 Answers1

0

Does this help? Otherwise, please show us what you have tried so far.

#some sample data
df = pd.DataFrame({
    'col1': [2, 1, 9, 8, 7, 4],
    'col2': [0, 1, 9, 4, 2, 3],
    'col3': ['A', 'C', 'B', 'E', 'D', 'C']
})

#sort values by col3
df = df.sort_values(by=['col3'])

Output of df

   col1  col2 col3
0     2     0    A
2     9     9    B
1     1     1    C
5     4     3    C
4     7     2    D
3     8     4    E
flyakite
  • 724
  • 3
  • 6
  • Thanks for your answer. I posted an edit above. – Terminator6677 Oct 17 '22 at 12:10
  • Some ideas: 1) Kindly check, if there really is no further line of code, which may have an impact. 2) May open a second Jupyter notebook and copy-paste the minimal example therein and check, if still the entire dataframe is not sorted. 3) Go from there, update the values directly inside the original dataframe via `df.sort_values(..., inplace=true)`, may define the sort type directly (e.g. with `kind=mergesort`). – flyakite Oct 18 '22 at 07:12