From b642ee797c4a303d86abb8a37d4325a51dca1bee Mon Sep 17 00:00:00 2001 From: onesandzeroes Date: Tue, 24 Jun 2014 18:40:39 +1000 Subject: [PATCH] DOC: Add cookbook recipe like R's `expand.grid()` Add `expand_grid()` recipe to cookbook Add note about cookbook recipe to release notes Revert "Add note about cookbook recipe to release notes" This reverts commit e431a024c2e688f9917cafb12235ee72ecc3e119. Remove pandas import, print dataframe in cookbook --- doc/source/cookbook.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/doc/source/cookbook.rst b/doc/source/cookbook.rst index b16c71bffd64d..283cf0438857a 100644 --- a/doc/source/cookbook.rst +++ b/doc/source/cookbook.rst @@ -663,3 +663,25 @@ To globally provide aliases for axis names, one can define these 2 functions: df2 = DataFrame(randn(3,2),columns=['c1','c2'],index=['i1','i2','i3']) df2.sum(axis='myaxis2') clear_axis_alias(DataFrame,'columns', 'myaxis2') + +Creating Example Data +--------------------- + +To create a dataframe from every combination of some given values, like R's ``expand.grid()`` +function, we can create a dict where the keys are column names and the values are lists +of the data values: + +.. ipython:: python + + import itertools + + def expand_grid(data_dict): + rows = itertools.product(*data_dict.values()) + return pd.DataFrame.from_records(rows, columns=data_dict.keys()) + + df = expand_grid( + {'height': [60, 70], + 'weight': [100, 140, 180], + 'sex': ['Male', 'Female']} + ) + df