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
Cobain
  • 195
  • 1
  • 14
  • 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. – benjwadams Dec 15 '15 at 18:54
  • Possible duplicate of [Python, pandas: how to sort dataframe by index](http://stackoverflow.com/questions/22211737/python-pandas-how-to-sort-dataframe-by-index) – TayTay Dec 15 '15 at 19:33

1 Answers1

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)
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • I have tried what you suggested, but get the following error: sort_index() got an unexpected keyword argument 'level' – Cobain Dec 15 '15 at 22:50
  • @Cobain please upgrade your pandas. Note: That argument is optional in this case: `BusStopList.sort_index(ascending=True)` should just work. – Andy Hayden Dec 15 '15 at 22:54