In [ ]:
%run setup_env.py
%matplotlib inline

Cookbook

pandas 0.15

Idioms


In [ ]:
df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C'])
df_mask = pd.DataFrame({
    'A': [True] * 10,
    'B': [False] * 10,
    'C': [True, False] * 5
})
df.where(df_mask, -1000)

In [ ]:
df['logic'] = np.where(df['A'] > 0, 'ok', 'not ok')
df

Splitting

df[df['A'] > 0]

Building Criteria

df.loc[(df['A'] > 0) | (df['B'] < 0), ['A', 'B']]

In [ ]:
import functools
crit = functools.reduce(lambda x, y: x & y, [df['A'] > 0, df['B'] > 0, df['C'] < 0])
df[crit]

Selection

DataFrames

df[(df['A'] > 1) & (df.index.isin(range(5)))]

Panels

New Columns


MultiIndexing

Arithmetic

Slicing

Sorting

Levels

Panelnd

Missing Data

Grouping

Timeseries

Merge

Plotting

Computation

Timedeltas

Aliasing Axis Names

Creating Example Data


In [25]:
rng = pd.date_range('2014/1/1', periods=100, freq='D')
data = np.random.randn(100, 4)
cols = ['A', 'B', 'C', 'D']
df1 = df2 = df3 = pd.DataFrame(data, index=rng, columns=cols)
pf = pd.Panel({'df1': df1, 'df2': df2, 'df3': df3})
pft = pf.transpose(2, 0, 1)
pft


Out[25]:
<class 'pandas.core.panel.Panel'>
Dimensions: 4 (items) x 3 (major_axis) x 100 (minor_axis)
Items axis: A to D
Major_axis axis: df1 to df3
Minor_axis axis: 2014-01-01 00:00:00 to 2014-04-10 00:00:00