在大数据框架中减少因子水平的 Pythonic 方法

Pythonic Way of Reducing Factor Levels in Large Dataframe

我正在尝试减少 pandas 数据框中一列内的因子级别数,以便任何因子的总实例占所有列行的比例低于定义的阈值(默认设置为1%), 将被分桶到标记为 'Other' 的新因子中。下面是我用来完成这个任务的函数:

def condenseMe(df, column_name, threshold = 0.01, newLabel = "Other"):

    valDict = dict(df[column_name].value_counts() / len(df[column_name]))
    toCondense = [v for v in valDict.keys() if valDict[v] < threshold]
    if 'Missing' in toCondense:
        toCondense.remove('Missing')
    df[column_name] = df[column_name].apply(lambda x: newLabel if x in toCondense else x)

我 运行 遇到的问题是我正在处理一个大型数据集(约 1800 万行),并试图在具有超过 10,000 个级别的列上使用此函数。因此,在此列上执行此功能需要很长时间才能完成。是否有更 pythonic 的方法来减少执行速度更快的因子级别的数量?任何帮助将不胜感激!

您可以结合使用 groupbytranformcount:

def condenseMe(df, col, threshold = 0.01, newLabel="Other"):
    # Create a new Series with the normalized value counts
    counts = df[[col]].groupby(col)[col].transform('count') / len(df)
    # Create a 1D mask based on threshold (ignoring "Missing")
    mask = (counts < threshold) & (df[col] != 'Missing')

    # Assign these masked values a new label
    df[col][mask] = newLabel