Given this sample dataframe named df:
df = pd.DataFrame({'name': ['Mary', 'Joe', 'Jessie'], 'score': [10, 3, 13]})
name score
Mary 10
Joe 3
Jessie 13
Now trying to sort the dataframe instead of by column, by the column via its location:
Typical way to sort dataframe:
df = df.sort_values['score']
Trying to sort it like this (which is not working):
df = df.sort_values[df.iloc[:, 1]]
This raises an otherwise unintelligible "Key Error" with no explanation what it is referring to.
I need to do this because the function containing this code will have a different name for the second column each time it runs so I cannot hard code a column name for sorting and instead need to sort by whatever the second column is, no matter its name.
Thanks for taking a moment to check this out.