deepaugment/lib/pandas_utilities.py
def rank_in_group(df, group_col, rank_col, rank_method="first"):
    """Ranks a column in each group which is grouped by another column
        
    Args:
        df (pandas.DataFrame): dataframe to rank-in-group its column
        group_col (str): column to be grouped by
        rank_col (str): column to be ranked for
        rank_method (str): rank method to be the "method" argument of pandas.rank() function
        
    Returns:
        pandas.DataFrame: dataframe after the rank-in-group operation
    """

    df = df.copy()
    df_slice = df[[group_col, rank_col]].drop_duplicates()
    df_slice["ranked_{}".format(rank_col)] = df_slice[rank_column].rank(
        method=rank_method
    )
    df = pd.merge(
        df,
        df_slice[[group_col, "ranked_{}".format(rank_col)]],
        how="left",
        on=group_col,
    )
    return df
Ranks a column in each group which is grouped by another column
        
    Args:
        df (pandas.DataFrame): dataframe to rank-in-group its column
        group_col (str): column to be grouped by
        rank_col (str): column to be ranked for
        rank_method (str): rank method to be the "method" argument of pandas.rank() function
        
    Returns:
        pandas.DataFrame: dataframe after the rank-in-group operation
    
Ranks a column in each group which is grouped by another column
Ranks a column in each group which is grouped by another column
def rank_in_group(df, group_col, rank_col, rank_method="first"):
    df = df.copy()
    df_slice = df[[group_col, rank_col]].drop_duplicates()
    df_slice["ranked_{}".format(rank_col)] = df_slice[rank_column].rank(
        method=rank_method
    )
    df = pd.merge(
        df,
        df_slice[[group_col, "ranked_{}".format(rank_col)]],
        how="left",
        on=group_col,
    )
    return df
def rank_in_group ( df , group_col , rank_col , rank_method = "first" ) : df = df . copy ( ) df_slice = df [ [ group_col , rank_col ] ] . drop_duplicates ( ) df_slice [ "ranked_{}" . format ( rank_col ) ] = df_slice [ rank_column ] . rank ( method = rank_method ) df = pd . merge ( df , df_slice [ [ group_col , "ranked_{}" . format ( rank_col ) ] ] , how = "left" , on = group_col , ) return df
Ranks a column in each group which is grouped by another column
Ranks a column in each group which is grouped by another column
"""Ranks a column in each group which is grouped by another column
        
    Args:
        df (pandas.DataFrame): dataframe to rank-in-group its column
        group_col (str): column to be grouped by
        rank_col (str): column to be ranked for
        rank_method (str): rank method to be the "method" argument of pandas.rank() function
        
    Returns:
        pandas.DataFrame: dataframe after the rank-in-group operation
    """
dataframe after the rank-in-group operation
dataframe after the rank - in - group operation
pandas.DataFrame
df
dataframe to rank-in-group its column
dataframe to rank - in - group its column
group_col
column to be grouped by
column to be grouped by
rank_col
column to be ranked for
column to be ranked for
rank_method
rank method to be the "method" argument of pandas.rank() function
rank method to be the " method " argument of pandas . rank () function