Created
February 10, 2015 02:55
-
-
Save ramhiser/95f9e1e46a38c5fd165e to your computer and use it in GitHub Desktop.
Fills a DataFrame with the Cartesian product of the given indices.
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
# Goal: Fill missing date/group pairs with fill_value using Cartesian product of indices | |
import pandas as pd | |
def fill_product(df, index, fill_value=0): | |
""" | |
Fills a DataFrame with the Cartesian product of the given indices. | |
See: http://stackoverflow.com/a/16994910/234233 | |
Example: | |
df = pd.DataFrame([['01-02-2015', 'a', 17], | |
['01-09-2015', 'a', 42], | |
['01-30-2015', 'a', 19], | |
['01-02-2015', 'b', 23], | |
['01-23-2015', 'b', 1], | |
['01-30-2015', 'b', 13]]) | |
df.columns = ['date', 'group', 'response'] | |
fill_product(df, ['date', 'group'], fill_value=0) | |
:param df: pandas DataFrame | |
:param index: df's columns to index | |
:param fill_value: missing values are filled with this value | |
:return: DataFrame filled wtih Cartesian product of indices | |
""" | |
df = df.set_index(index) | |
idx_product = pd.MultiIndex.from_product(df.index.levels, names=df.index.names) | |
return df.reindex(idx_product, fill_value=fill_value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment