Pandas Example


This example shows how to treat basic data with pandas and how to visualise it (inspiration taken here).

In [3]:
import pandas as pd

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
%matplotlib inline
In [4]:
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
ts
Out[4]:
2000-01-01     0.005206
2000-01-02    -1.188286
2000-01-03    -0.816645
2000-01-04     1.083017
2000-01-05     2.736275
2000-01-06     5.149380
2000-01-07     6.669660
2000-01-08     6.361551
2000-01-09     6.747546
2000-01-10     7.419969
2000-01-11     6.444654
2000-01-12     7.501025
2000-01-13     5.835791
2000-01-14     6.338942
2000-01-15     7.049053
2000-01-16     5.906274
2000-01-17     6.565807
2000-01-18     7.543933
2000-01-19     6.823760
2000-01-20     7.980036
2000-01-21     7.962416
2000-01-22     7.925898
2000-01-23     6.980807
2000-01-24     5.734831
2000-01-25     7.012284
2000-01-26     8.056432
2000-01-27     8.400838
2000-01-28     8.163988
2000-01-29     8.162203
2000-01-30    10.005383
                ...    
2002-08-28   -16.758419
2002-08-29   -16.662693
2002-08-30   -16.421452
2002-08-31   -16.731040
2002-09-01   -14.263456
2002-09-02   -13.786873
2002-09-03   -15.246819
2002-09-04   -14.174266
2002-09-05   -12.745690
2002-09-06   -12.745959
2002-09-07   -12.533763
2002-09-08   -12.759596
2002-09-09   -12.784212
2002-09-10   -12.897230
2002-09-11   -12.316893
2002-09-12    -9.805400
2002-09-13    -9.076824
2002-09-14    -7.626493
2002-09-15    -8.768385
2002-09-16   -10.291610
2002-09-17    -9.757074
2002-09-18   -10.162365
2002-09-19    -9.566291
2002-09-20   -10.111748
2002-09-21    -9.594906
2002-09-22    -9.620807
2002-09-23    -8.704808
2002-09-24    -8.596246
2002-09-25    -8.847098
2002-09-26    -8.843625
Freq: D, dtype: float64
In [5]:
ts.plot()
Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f8d0c019750>
In [7]:
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()
plt.figure(); df.plot();
<matplotlib.figure.Figure at 0x7f8ce776fd10>
In [8]:
plt.figure()
df.ix[5].plot(kind='bar'); plt.axhline(0, color='k')
Out[8]:
<matplotlib.lines.Line2D at 0x7f8ce744cd90>