【问题标题】:Pandas - Loop through groupby df and filter by intersectionPandas - 遍历 groupby df 并按交集过滤
【发布时间】:2018-12-09 01:17:48
【问题描述】:

我无法在 Pandas 中组合以下步骤:我有 2 个实体的按日期快照。我发现每个日期的 2 个实体之间的对象交集,这些对象存储在列表列表中(每个日期一个子列表)。

我现在想过滤每个实体的原始数据框以仅考虑交叉点,因此我尝试使用布尔索引进行过滤,同时也使用 groupby。请参阅下面的我正在尝试构建的循环:

filtered_df=pd.DataFrame()
for date_sublist in range(len(intersect_list):
    overlap_temp=df_orig[df_orig['ObjectName'].filter(intersect_list[date_sublist])]
    bkln_overlap.append(overlap_temp)

我还尝试了下面的构造作为测试,我试图只保留对象名称与特定交集列表匹配的行:

df_orig[df_orig['ObjectName'] in intersect_list[1]]

有人对这个问题有什么建议吗?谢谢你。

【问题讨论】:

  • 如果您可以发布示例df,那将很有帮助。

标签: python pandas filter group-by


【解决方案1】:

没有来自OP的样本数据,我将使用一个简单的例子来演示。我希望这是你所追求的,或者至少可以稍微修改一下以达到你想要的效果。

再次阅读您的 OP(连同您的 cmets)后,我认为您应该将交集列表存储在字典中,如下所示:

intersections = {'01/01/2018': ['ObjectA','ObjectC'], '01/02/2018': ['ObjectA','ObjectD'], etc.....}

要实现这一点:

df = pd.DataFrame([['01/01/2018', 'ObjectA', 0, 0, 0, 1],['01/01/2018', 'ObjectE', 0, 1, 1, 1],['01/02/2018', 'ObjectB', 0, 0, 0, 0],
                ['01/04/2018', 'ObjectD', 0, 1, 1, 0],['01/02/2018', 'ObjectE', 1, 1, 0, 1],['01/03/2018', 'ObjectB', 0, 0, 0, 0],
                ['01/01/2018', 'ObjectC', 0, 1, 1, 0],['01/03/2018', 'ObjectA', 1, 1, 0, 1],['01/04/2018', 'ObjectD', 0, 0, 0, 0]],
                columns=['Date','Object','x1','x2','x3','x4'])

         Date   Object  x1  x2  x3  x4
0  01/01/2018  ObjectA   0   0   0   1
1  01/01/2018  ObjectE   0   1   1   1
2  01/02/2018  ObjectB   0   0   0   0
3  01/04/2018  ObjectD   0   1   1   0
4  01/02/2018  ObjectE   1   1   0   1
5  01/03/2018  ObjectB   0   0   0   0
6  01/01/2018  ObjectC   0   1   1   0
7  01/03/2018  ObjectA   1   1   0   1
8  01/04/2018  ObjectD   0   0   0   0

'Date'分组:

grouped = df.groupby('Date')
intersections = {key: list(set(grouped.get_group(key)['Object'])) for key, val in grouped}

给予:

{'01/01/2018': ['ObjectE', 'ObjectA', 'ObjectC'], '01/02/2018': ['ObjectE', 'ObjectB'], '01/03/2018': ['ObjectA', 'ObjectB'], '01/04/2018': ['ObjectD']}

然后应用交集字典中的过滤器:

out = [df[(df['Date']==key) & (df['Object'].isin(val))] for key, val in intersections.items()]

给予:

         Date   Object  x1  x2  x3  x4
0  01/01/2018  ObjectA   0   0   0   1
1  01/01/2018  ObjectE   0   1   1   1
6  01/01/2018  ObjectC   0   1   1   0
         Date   Object  x1  x2  x3  x4
2  01/02/2018  ObjectB   0   0   0   0
4  01/02/2018  ObjectE   1   1   0   1
         Date   Object  x1  x2  x3  x4
5  01/03/2018  ObjectB   0   0   0   0
7  01/03/2018  ObjectA   1   1   0   1
         Date   Object  x1  x2  x3  x4
3  01/04/2018  ObjectD   0   1   1   0
8  01/04/2018  ObjectD   0   0   0   0

【讨论】:

  • 谢谢。每个日期实例的交集都会发生变化,这使得它变得更加困难。因此,我需要遍历每个按日期分组的数据框以及该时间段的特定交集。这有意义吗?
  • 那么每个日期都有一个单独的数据框?
  • 我有一个包含所有日期的数据框。我按日期分组以创建对象交集列表。所以我已经有了每个日期的交集列表。我现在需要通过相交对象列表过滤每个日期组
  • 过滤之后我会用字典做什么?
猜你喜欢
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 2014-10-28
  • 2021-07-05
  • 2022-11-18
  • 2021-09-12
  • 2018-05-13
  • 1970-01-01
相关资源
最近更新 更多