繁体   English   中英

在大型数据框中减少因子级别的 Pythonic 方法

[英]Pythonic Way of Reducing Factor Levels in Large Dataframe

我正在尝试减少 Pandas 数据框中列中的因子级别数,以便将任何因子的总实例作为低于定义阈值(默认设置为 1%)的所有列行的比例,将被分入一个标记为“其他”的新因素。 以下是我用来完成此任务的函数:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM