def normalize_trans_probs(p):
    """
    Normalize a set of transition probabilities.

    Parameters
    ----------
    p : pandas.DataFrame, dtype float
        Unnormalized transition probabilities. Indexed by
        source_level_idx, destination_level_idx.

    Returns
    -------
    pandas.DataFrame, dtype float
        Normalized transition probabilities: the sum of
        all probabilites with the same source_level_idx sum to one.
        Indexed by source_level_idx, destination_level_idx.
    """
    p_summed = p.groupby(level=0).sum()
    index = p.index.get_level_values("source_level_idx")
    p_norm = p / p_summed.loc[index].values
    p_norm = p_norm.fillna(0.0)
    return p_norm