def sample_population(trips_df, sample_perc, attributes_df=None, weight_col='freq'):
    """
    Return the trips of a random sample of the travel population.
    We merge the trips and attribute datasets to enable probability weights based on population demographics.

    :params DataFrame trips_df: Trips dataset
    :params DataFrame attributes_df: Population attributes dataset.
    :params float sample_perc: Sampling percentage
    :params string weight_col: The field to use for probability weighting

    :return: Pandas DataFrame, a sampled version of the trips_df dataframe
    """
    if attributes_df is not None:
        sample_pids = trips_df.groupby('pid')[['freq']].sum().join(
            attributes_df, how='left'
        ).sample(
            frac=sample_perc, weights=weight_col
            ).index
    else:
        sample_pids = trips_df.groupby('pid')[['freq']].sum().sample(
            frac=sample_perc, weights=weight_col
            ).index

    return trips_df[trips_df.pid.isin(sample_pids)]