In [1]:
from pandas import DataFrame
import pandas as pd
%matplotlib inline
In [2]:
print 'Pandas Version: ' + pd.__version__
Pandas Version: 0.13.0rc1

Sort

How do I sort a column ascending?

In [3]:
# dataframe
df = DataFrame(data={'col1':[0,10,2,30,4]})
df
Out[3]:
col1
0 0
1 10
2 2
3 30
4 4

5 rows × 1 columns

In [4]:
# Sort by col1 ascending
df.sort(columns='col1')
Out[4]:
col1
0 0
2 2
4 4
1 10
3 30

5 rows × 1 columns

How do I sort by the index?

In [5]:
# Unordered index

d = {'col2':[22,10,113]}

i = [pd.Timestamp('20130102'),
     pd.Timestamp('2013-01-01'),
     pd.Timestamp('1/3/2013')]
                 

df = DataFrame(data=d, index = i)
df
Out[5]:
col2
2013-01-02 22
2013-01-01 10
2013-01-03 113

3 rows × 1 columns

In [6]:
# Index sorted ascending
df.sort_index()
Out[6]:
col2
2013-01-01 10
2013-01-02 22
2013-01-03 113

3 rows × 1 columns

Author: David Rojas LLC