Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading