0

I have data built into the form of:

Node    |   Depends_On
Node_3     Node_2
Node_2     Node_1
Node_4     Node_1    

I have put this into a pandas dataframe. I want to get a table:

Node    |   Level
Node_1        1
Node_2        2
Node_3        3
Node_4        2

I know I can achieve this in Python using recursion function but I'm not really sure how to start.

I tried to to modify the answer to this: Recursive SQL CTE query in Pandas?

But I get key errors. Is it because I don't have numeric entries?

Community
  • 1
  • 1
KillerSnail
  • 3,321
  • 11
  • 46
  • 64

1 Answers1

0

I know I can achieve this in Python using recursion function but I'm not really sure how to start.

Start by finding an algorithm which gives you the level of one node.

For example:

  1. Does the node depend on another node?

  2. If not, the level is 1.

  3. Otherwise, the level is the level of the other node plus 1.

This should be relatively straightforward to translate to a Python function.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • So in pseudo code I can: Build list of unique entities set all to level 1. Then check if each entry is in Node column. If it is increment by +1. Subset dataframe by values that have increased in value and repeat? – KillerSnail May 07 '15 at 08:47