def explode(df, amount_col, index_col='index', keep_index=False):
    """
    Given a dataframe with an capacity column,
    generate a single row for each available unit.
    The new data frame has a unique index with the old
    index preserved in the specified column.

    Parameters:
    -----------
    df: pandas.DataFrame
        Rows to explode.
    amount_col: str
        Column with amounts to explode/repeat.
    index_col: str, optional, default 'index'
        Name of column that will hold the previous
        index.
    keep_index: optional, defualt False
        If True, the original index is preserved.
        If False, the index is copied to a column.
    """
    candidates = df[df[amount_col] > 0]
    repeat_idx = candidates.index.repeat(candidates[amount_col].astype(int))
    exploded = candidates.reindex(repeat_idx)

    if not keep_index:
        exploded[index_col] = exploded.index.values
        exploded.reset_index(drop=True, inplace=True)

    return exploded