def df_set_diag(df, val, copy=True):
    """
    Sets the diagonal of a dataframe to a value. Diagonal in this case is anything where row label == column label.

    :param df: pd.DataFrame
        DataFrame to modify
    :param val: numeric
        Value to insert into any cells where row label == column label
    :param copy: bool
        Force-copy the dataframe instead of modifying in place
    :return: pd.DataFrame / int
        Return either the modified dataframe (if copied) or the number of cells modified (if changed in-place)
    """
    isect = df.index.intersection(df.columns)
    if copy:
        df = df.copy()
    for i in range(len(isect)):
        df.loc[isect[i], isect[i]] = val
    if copy:
        return df
    else:
        return len(isect)
