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?
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With