Skip to content

DOC: Cookbook recipe for emulating R's expand.grid() (#7426) #7556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2014

Conversation

onesandzeroes
Copy link
Contributor

closes #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:

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

@jreback jreback changed the title DOC: Cookbook recipe for emulating R's expand.grid() (#7426) DOC: Cookbook recipe for emulating R's expand.grid() (#7426) Jun 24, 2014
@jreback jreback added the Docs label Jun 24, 2014
@@ -135,6 +135,7 @@ Enhancements

- ``Period`` and ``PeriodIndex`` can contain ``NaT`` in its values (:issue:`7485`)

- Quick cookbook recipe for emulating R's ``expand.grid()``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can remove this line

@jreback
Copy link
Contributor

jreback commented Jun 24, 2014

@jorisvandenbossche ?

{'height': [60, 70],
'weight': [100, 140, 180],
'sex': ['Male', 'Female']}
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add df on a new line? (so the resulting dataframes is also shown in the docs)

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
@onesandzeroes
Copy link
Contributor Author

@jreback Alright, I've made the suggested changes and squashed down to a single commit, let me know if there's anything more I'd need to do.

@jreback jreback added this to the 0.14.1 milestone Jun 25, 2014
@jorisvandenbossche
Copy link
Member

Looking good, thanks!

jorisvandenbossche added a commit that referenced this pull request Jun 25, 2014
DOC: Cookbook recipe for emulating R's expand.grid() (#7426)
@jorisvandenbossche jorisvandenbossche merged commit 69bb0e8 into pandas-dev:master Jun 25, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: Create dataframes using every combination of given values, like R's expand.grid()
3 participants