Last active
March 22, 2018 14:31
-
-
Save yassineAlouini/831692a059e0515a9ff6d33ebe23d9ba to your computer and use it in GitHub Desktop.
Create a DataFrame from all the possible combination of a dict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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