Skip to content

Instantly share code, notes, and snippets.

@yassineAlouini
Last active March 22, 2018 14:31
Show Gist options
  • Save yassineAlouini/831692a059e0515a9ff6d33ebe23d9ba to your computer and use it in GitHub Desktop.
Save yassineAlouini/831692a059e0515a9ff6d33ebe23d9ba to your computer and use it in GitHub Desktop.
Create a DataFrame from all the possible combination of a dict
import pandas as pd
import itertools
import numpy as np
# From here: https://pandas.pydata.org/pandas-docs/stable/cookbook.html#creating-example-data
def expand_grid(data_dict):
rows = itertools.product(*data_dict.values())
return pd.DataFrame.from_records(rows, columns=data_dict.keys())
d = {'sid': [1, 2, 3], 'tms_gmt': pd.date_range('2017-1-1', '2018-1-1'),
'bikes': [None, 10, 3, 2, 4, np.nan]}
df = expand_grid(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment