Python Pandas - Date Column to Column index


Question

I have a table of data imported from a CSV file into a DataFrame.

The data contains around 10 categorical fields, 1 month column (in date time format) and the rest are data series.

How do I convert the date column into an index across the the column axis?

1
18
4/1/2013 10:47:25 PM

You can use set_index:

df.set_index('month')

For example:

In [1]: df = pd.DataFrame([[1, datetime(2011,1,1)], [2, datetime(2011,1,2)]], columns=['a', 'b'])

In [2]: df
Out[2]: 
   a                   b
0  1 2011-01-01 00:00:00
1  2 2011-01-02 00:00:00

In [3]: df.set_index('b')
Out[3]: 
            a
b            
2011-01-01  1
2011-01-02  2
26
4/1/2013 9:54:37 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon