我无法在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]]
有人对此问题有任何建议吗?谢谢。
答案 0 :(得分:0)
在没有OP的示例数据的情况下,我将使用一个简单的示例进行演示。我希望这是您的追求,或者至少可以对其稍加修改以实现您想要的。
在再次阅读您的OP(以及您的注释)之后,我认为您应该将相交列表存储在字典中,如下所示:
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