Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 17071

Finding node depth from Pandas dataframe for full hierarchical data

$
0
0

I have a Pandas dataframe with sample hierarchical data i.e.

data = pd.DataFrame({"manager_id": ["A", "A", "B", "A", "C", "A", "B", "F"],"employee_id": ["B", "C", "C", "D", "E", "E", "E", "G"]} )

Given that the data consists of all the descendent relationships for each manager. For example in each manager id (e.g. "A"), the employee id comprises both the employees (e.g. "B") directly managed by manager "A" and the employees managed by employee "B" (e.g. "C", "D"). I already have a solution to create a graph in networkx (How to generate graph from Pandas dataframe with full hierachical data?). But how do I add a column in this data that computes the depth of each manager and employee from the graph?

Intended output as below:

  manager_id employee_id  depth0          A           B      11          A           C      22          B           C      13          A           D      14          C           E      15          A           E      36          B           E      27          F           G      1

Viewing all articles
Browse latest Browse all 17071

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>