1

I have the following dataframe:

Join_Count  1
LSOA11CD    
E01006512   15
E01006513   35
E01006514   11
E01006515   11
E01006518   11
...

But when I try to sort it:

BusStopList.sort("LSOA11CD",ascending=1)

I get the following:

Key Error: 'LSOA11CD'

How do I go about sorting this by either the LSOA column or the column full of numbers which doesn't have a heading?


The following is the information produced by Python about this dataframe:

<class 'pandas.core.frame.DataFrame'>
Index: 286 entries, E01006512 to E01033768
Data columns (total 1 columns):
1    286 non-null int64
dtypes: int64(1)
memory usage: 4.5+ KB
2
  • From the position of the label, it looks like LSOA11CD might be an index, rather than a column, which as I recall is what the DataFrame.sort method is looking for. What does print(BusStopList.index) return? If it is indeed an index, you'll want to use sort_index instead. Commented Dec 15, 2015 at 18:54
  • Possible duplicate of Python, pandas: how to sort dataframe by index Commented Dec 15, 2015 at 19:33

1 Answer 1

2

'LSOA11CD' is the name of the index, 1 is the name of the column. So you must use sort index (rather than sort_values):

BusStopList.sort_index(level="LSOA11CD", ascending=True)
Sign up to request clarification or add additional context in comments.

2 Comments

I have tried what you suggested, but get the following error: sort_index() got an unexpected keyword argument 'level'
@Cobain please upgrade your pandas. Note: That argument is optional in this case: BusStopList.sort_index(ascending=True) should just work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.