user19996190
user19996190

Reputation: 1

Create a unique id for each parent child path - Python

I'm currently learning python and I'm stuck to resolve a problem. 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). Something like this:enter image description here I tried several methods unsuccessfully. Do you have some ideas? Thank you

Upvotes: 0

Views: 105

Answers (1)

darren
darren

Reputation: 5764

You could put this into a dataframe or a dictionary.

Here is the dataframe example:

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:

enter image description here

Upvotes: 0

Related Questions