pydata/pandas — pull request #7556 by onesandzeroes (2014-06-24 08:49:09+00:00)
Title: DOC: Cookbook recipe for emulating R's `expand.grid()` (#7426)

As suggested by @jreback in #7426, I've added a quick cookbook recipe for emulating R's `expand.grid()`, which creates a dataframe from every combination of the values you give it.

None of the existing sections really seemed to apply, so I've added it in a new section.

Example usage:

```python
import itertools
import pandas as pd

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
Out[2]:
sex weight height
0 Male 100 60
1 Male 100 70
2 Male 140 60
3 Male 140 70
4 Male 180 60
5 Male 180 70
6 Female 100 60
7 Female 100 70
8 Female 140 60
9 Female 140 70
10 Female 180 60
11 Female 180 70
```