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?
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