首页 > 解决方案 > 在 Pandas 中对类似项目进行分组

问题描述

我正在尝试做某事,我想知道这是否可以在 Pandas 中完成,或者是否有更好的工具来完成这项工作(目前我只是使用直接 python)。以下是起始数据:

# We have a listing of files for the movie Titanic
# And we want to break them into groups of similar titles,
# To see which of those are possible duplicates.
import pandas as pd
titanic_files = [
    {"File": "Titanic_HD2398.mov",  "Resolution": "HD", "FrameRate": 23.98, "Runtime": 102},
    {"File": "Titanic1.mov",        "Resolution": "SD", "FrameRate": 23.98, "Runtime": 102},
    {"File": "Titanic1.mov",        "Resolution": "HD", "FrameRate": 23.98, "Runtime": 102},
    {"File": "Titanic.mov",         "Resolution": "HD", "FrameRate": 24.00, "Runtime": 103},
    {"File": "MY_HD2398.mov",       "Resolution": "HD", "FrameRate": 23.98, "Runtime": 102}
]
df = pd.DataFrame(titanic_files)

我想按相似的数据对这些文件进行分组,从不折叠行级数据,例如:

  1. 第 1 步——按分辨率分组

    
    ---- HD ----
    File               Resolution             FrameRate              RunTime
    Titanic_HD2398.mov HD                     23.98                  102
    Titanic1.mov       HD                     23.98                  102
    Titanic.mov        HD                     24.00                  103
    MY_HD2398.mov      HD                     23.98                  102
    
    ---- SD ----
    File               Resolution             FrameRate              RunTime
    Titanic1.mov       SD                     23.98                  102
    
  2. 第 2 步——按帧率分组

    ---- HD -----------------------
     +----------- 23.98 ------------
    File               Resolution             FrameRate              RunTime
    Titanic_HD2398.mov HD                     23.98                  102
    Titanic1.mov       HD                     23.98                  102
    MY_HD2398.mov      HD                     23.98                  102
    
     +----------- 24.00 ------------
    File               Resolution             FrameRate              RunTime
    Titanic.mov        HD                     24.00                  103
    
    
    ---- SD -----------------------
     + ---------- 23.98 ------------
    
    File               Resolution             FrameRate              RunTime
    Titanic1.mov       SD                     23.98                  102
    

最后,我想基本上为每个最小的分组都有单独的数据框。在 python 中,我目前正在使用以下数据结构执行此操作:

{
   'GroupingKeys': [{File1WithinThatBucket}, {File2WithinThatBucket}, ...]
}

例如:

{
   'HD+23.98' + [{'File': ...}],
   'HD+24.00' + [{'File': ...}]
}

另外,请记住,我要分组的字段大约有 10-15 个,我在上面的问题中只包括了两个,所以这种方法需要非常通用(另外,一些匹配标准不是确切地说,例如运行时可能会被存储为 +/- 2 秒,某些值可能为空,等等)。

回到最初的问题:在 Pandas 中可以做这样的事情吗?如果可以,怎么做?

标签: pythonpandasgroup-byaggregation

解决方案


Pandasgroupby似乎是要使用的工具,它可以根据需要使用任意数量的 groupers,它们可以是 list、series、column_name、index_level、callable 的类型......你可以命名它

例如,您可以这样做:

df = df.groupby(
    [
        'Resolution', df.FrameRate//0.02 * 0.02,
        pd.cut(df.Runtime, bins=[45, 90, 95, 100, 120])
    ]
).File.apply(list)

这将返回一个具有 3 级和一列的唯一 MultiIndex 的 DataFrame,每行包含一个文件名列表。

如果出于某种原因,您还可以获取每个组的完整行,使用其他数据,您希望将一个 df 拆分为多个并保持这种方式。

for group_id, group_rows in df.groupby(...):
    # group id are tuples each with a unique combination of the grouping vectors
    # group_rows is a df of the matching rows, with the same columns as df

推荐阅读