def _GetTopKItems(df, col_user, col_rating, k):
    """Get the top k items for each user.

    Params:
        dataframe (pandas.DataFrame): DataFrame of rating data
        col_user (str): column name for user
        col_rating (str): column name for rating
        k (int): number of items for each user

    Returns:
        pd.DataFrame: DataFrame of top k items for each user, sorted by `col_user` and `rank`
    """
    top_k_items = df.groupby(col_user, as_index=False).apply(lambda x: x.
        nlargest(k, col_rating)).reset_index(drop=True)
    top_k_items['rank'] = top_k_items.groupby(col_user, sort=False).cumcount(
        ) + 1
    return top_k_items
