大Pandas中相似项目的分组

2024-04-28 08:28:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试做一些事情,我想知道这是否可以在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秒,一些值可能为空,等等)

回到最初的问题:这样的事情可以在熊猫身上做吗?如果可以,怎么做


Tags: of数据myfilessdfileruntimemov
1条回答
网友
1楼 · 发布于 2024-04-28 08:28:13

Pandas groupby似乎是要使用的工具,它可以根据需要使用任意多个分组,它们可以是列表、系列、列名、索引级别、可调用类型。。。随便你说

例如,您可以执行以下操作:

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

它将返回一个数据帧,该数据帧具有3个级别的唯一多索引和一列,每行包含一个文件名列表

如果出于某种原因,使用其他数据,希望将一个df拆分为多个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

相关问题 更多 >