简体   繁体   English

为每个父子路径创建一个唯一的 id - Python

[英]Create a unique id for each parent child path - Python

I'm currently learning python and I'm stuck to resolve a problem.我目前正在学习 python 并且我坚持要解决一个问题。 I have a simple genealogy table (2 columns: parent_id, child_id), I would like to create a raw for each path and create a unique ID (keep the same ID if last child is the same).我有一个简单的家谱表(2 列:parent_id,child_id),我想为每个路径创建一个 raw 并创建一个唯一的 ID(如果最后一个孩子相同,则保持相同的 ID)。 Something like this: enter image description here I tried several methods unsuccessfully.像这样:在此处输入图像描述我尝试了几种方法均未成功。 Do you have some ideas?你有什么想法吗? Thank you谢谢

You could put this into a dataframe or a dictionary.您可以将其放入 dataframe 或字典中。

Here is the dataframe example:这是 dataframe 示例:

import io
import pandas as pd


# example dable
x = '''
parent, child 1, child 2, child 3, child 4, id
a, b, c, d, e, Id1  
g, f, e, , , Id1
h, i, j, k, , Id2
i, j, m, k, , Id2
'''

# data into a dataframe
data = io.StringIO(x)
df = pd.read_csv(data, sep=', ')

df

which returns this:它返回这个:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM