tong zhu
tong zhu

Reputation: 2043

How can I plot a dataframe with date as the columns name and time as the row lable?

I have a dataframe like this:enter image description here

Is it possible that I plot a graph that can show all the data in the dataframe and the x-lable is like: 2014-2-12-00:00,2014-2-13-00:05?

Upvotes: 0

Views: 64

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210972

Assuming we have the following DF:

In [159]: df
Out[159]:
       2014-02-12  2014-02-13  2014-02-14
00:00       19.74       24.76       24.83
00:15       20.21       25.41       19.19

Let's create a helper DF:

In [160]: x = df.T.stack().reset_index(name='val')

In [161]: x
Out[161]:
      level_0 level_1    val
0  2014-02-12   00:00  19.74
1  2014-02-12   00:15  20.21
2  2014-02-13   00:00  24.76
3  2014-02-13   00:15  25.41
4  2014-02-14   00:00  24.83
5  2014-02-14   00:15  19.19

In [162]: x['Date'] = pd.to_datetime(x.pop('level_0') + ' ' + x.pop('level_1'))

In [163]: x
Out[163]:
     val                Date
0  19.74 2014-02-12 00:00:00
1  20.21 2014-02-12 00:15:00
2  24.76 2014-02-13 00:00:00
3  25.41 2014-02-13 00:15:00
4  24.83 2014-02-14 00:00:00
5  19.19 2014-02-14 00:15:00

now we can easily plot it:

In [165]: import matplotlib
     ...: matplotlib.style.use('ggplot')
     ...:

In [166]: x.plot(x='Date', y='val')
Out[166]: <matplotlib.axes._subplots.AxesSubplot at 0xc03ce10>

enter image description here

Upvotes: 2

Related Questions