【问题标题】:Replacing a row value with the most frequent value in Pandas Dataframe用 Pandas Dataframe 中出现频率最高的值替换行值
【发布时间】:2018-03-24 13:21:08
【问题描述】:

我的数据框如下:

   |            types | freq   |     TypeList
0  |    Q11424 (item) |   29   |   Q11424 (item),Q571 (item)
1  |      Q571 (item) |   9    |   Q11424 (item),Q571 (item)
0  |    Q11012 (item) |   6    |   Q11012 (item)
0  |  Q4830453 (item) |   39   |   Q4830453 (item)
0  |  Q7725634 (item) |   2    |   Q7725634 (item),Q571 (item)
1  |      Q571 (item) |   9    |   Q7725634 (item),Q571 (item)
0  |   Q785479 (item) |   1    |   Q785479 (item),Q1344 (item)
1  |     Q1344 (item) |   1    |   Q785479 (item),Q1344 (item)

“类型”列实际上是“类型列表”的扁平列。 freq 列表示列类型中每个值的频率。这些频率来自整个数据帧。在这里,我只是添加了其中的几行。例如。 Q571 在类型列中出现了 9 次,因此 freq=9。 TypeList 列是每条记录的类型列表。如果 TypeList 列将包含一种以上的类型,我想添加新列 SuperType,它将具有最常见的类型。例如。我想要以下结果:

   |            types | freq   |     TypeList                   |SuperType
0  |    Q11424 (item) |   29   |   Q11424 (item),Q571 (item)    | Q11424
1  |      Q571 (item) |   9    |   Q11424 (item),Q571 (item)    | Q11424
0  |    Q11012 (item) |   6    |   Q11012 (item)                | Q11012
0  |  Q4830453 (item) |   39   |   Q4830453 (item)              | Q4830453
0  |  Q7725634 (item) |   2    |   Q7725634 (item),Q571 (item)  | Q571
1  |      Q571 (item) |   9    |   Q7725634 (item),Q571 (item)  | Q571
0  |   Q785479 (item) |   1    |   Q785479 (item),Q1344 (item)  | Q785479
1  |     Q1344 (item) |   1    |   Q785479 (item),Q1344 (item)  | Q785479

在第一行,TypeList 列的值为“Q11424 (item),Q571 (item)”。所以我想检查这两种类型的频率,即 29 和 9。并在该行的 superType 列中分配最常见的类型,即本例中的 Q11424。

【问题讨论】:

    标签: python pandas dataframe group-by max


    【解决方案1】:

    通过使用transform

    df['SuperType']=df.sort_values('freq').groupby('TypeList')['types'].transform('last')
    df['SuperType']=df.SuperType.str[:-6]
    df.sort_index()
    Out[1124]: 
                 types  freq                     TypeList  SuperType
    0    Q11424 (item)    29    Q11424 (item),Q571 (item)    Q11424 
    1      Q571 (item)     9    Q11424 (item),Q571 (item)    Q11424 
    2    Q11012 (item)     6                Q11012 (item)    Q11012 
    3  Q4830453 (item)    39              Q4830453 (item)  Q4830453 
    4  Q7725634 (item)     2  Q7725634 (item),Q571 (item)      Q571 
    5      Q571 (item)     9  Q7725634 (item),Q571 (item)      Q571 
    6   Q785479 (item)     1  Q785479 (item),Q1344 (item)     Q1344 
    7     Q1344 (item)     1  Q785479 (item),Q1344 (item)     Q1344 
    

    编辑:

    df=df.sort_values('freq')
    df['SuperType']=df.groupby('TypeList')['types'].transform('last').values
    df['SuperType']=df.SuperType.str[:-6]
    

    【讨论】:

    • 执行第一行后出现“ValueError: cannot reindex from a duplicate axis”异常。仅当我尝试将值分配给 df['superType'] 时才会发生异常,否则它会起作用。
    • 完美运行。非常感谢! :) 但是你能解释一下你到底做了什么吗?什么是 tranform('last') 以及为什么是 str[:-6]?
    • for transform :pandas.pydata.org/pandas-docs/stable/generated/… for str[:-6] 将字符串从最后 6 个位置切片到开头
    猜你喜欢
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 2019-08-15
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    相关资源
    最近更新 更多