How do I stop Key Error on df.sort_values()?

Question:

I can’t seem to find an answer to this particular error. I am trying to do a simple sort on my dataframe but every time I do it gives a Key Error. Here is my code:

import numpy as np
import pandas as pd

scores = [97, 91, 95, 88, 67, 65]
weights = [.5, .2, .1, .1, .05, .05]

##Put this into a DataFrame
df = pd.DataFrame(columns = ['Scores','Weights'])
df.Scores = scores
df.Weights = weights

df.sort_values(df['Scores'])

Every time I run this I get:

KeyError: 0    97
1    91
2    95
3    88
4    67
5    65
Name: Scores, dtype: int64

Can anyone identify what I’m doing wrong?

Asked By: Alex

||

Answers:

1 – In case you want to sort entire DataFrame based on a specific column, should fill the parameter as follows :

df.sort_values(by='Scores')
   Scores  Weights
5      65     0.05
4      67     0.05
3      88     0.10
1      91     0.20
2      95     0.10
0      97     0.50

2 – In case you only want to sort Series, you should write it as follows :

df['Scores'].sort_values()
5    65
4    67
3    88
1    91
2    95
0    97
Name: Scores, dtype: int64
Answered By: Laurent B.
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.