首页 > 解决方案 > 仅在数据框中保留直接父子 ID 对

问题描述

我有以下数据框:

   id_parent  id_child
0       1100      1090
1       1100      1080
2       1100      1070
3       1100      1060
4       1090      1080
5       1090      1070
6       1080      1070

我只想保持直接的父子连接。示例:1100 有 3 个连接,但仅应保留 1090,因为 1080 和 1070 已经是 1090 的子节点。此示例 df 仅包含 1 个样本,df 由多个父/子集群组成。

因此输出应如下所示:

   id_parent  id_child
0       1100      1090
1       1090      1080
2       1080      1070
3       1100      1060

示例代码:

import pandas as pd

#create sample input 
df_input = pd.DataFrame.from_dict({'id_parent': {0: 1100, 1: 1100, 2: 1100, 3: 1100, 4: 1090, 5: 1090, 6: 1080}, 'id_child': {0: 1090, 1: 1080, 2: 1070, 3: 1060, 4: 1080, 5: 1070, 6: 1070}})

#create sample output
df_output = pd.DataFrame.from_dict({'id_parent': {0: 1100, 1: 1090, 2: 1080, 3: 1100}, 'id_child': {0: 1090, 1: 1080, 2: 1070, 3: 1060}})

我目前的方法将基于这个问题:Creating dictionary of parent child pairs in pandas dataframe 但也许有一种简单干净的方法可以解决这个问题,而无需依赖额外的非标准库?

标签: pythondataframe

解决方案


这对我有用:

# First: group df by child id
grouped  = df_input.groupby(['id_child'], as_index=True).apply(lambda a: a[:])
# Second: Create a new output dataframe
OUTPUT = pd.DataFrame(columns=['id_parent','id_child'])
# Third: Fill it with the unique childs ids and the minimun id for their parent in case of more than one. 
for i,id_ch in enumerate(df_input.id_child.unique()):
    OUTPUT.loc[i] = [min(grouped.loc[id_ch].id_parent), id_ch]

推荐阅读