Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas - Date Column to Column index

Tags:

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?

like image 988
MrHopko Avatar asked Apr 01 '13 21:04

MrHopko


2 Answers

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 
like image 53
Andy Hayden Avatar answered Sep 27 '22 19:09

Andy Hayden


I had similar problem I've just solved by reset_index. But you can use either set_index or reset_index:

df_ind = df.set_index(['A', 'B']) 

Or

df.reset_index(level=0, inplace=True) 
like image 35
aysebilgegunduz Avatar answered Sep 27 '22 20:09

aysebilgegunduz