5 of 8
added 380 characters in body
misguided
  • 3.7k
  • 22
  • 54
  • 96

Tree View Iterations Python -- TypeError: 'float' object is not subscriptable

I am trying to look at 3 columns and create a iterative tree list.

My List is :

enter image description here

Result I want is :

enter image description here

Code I am trying to use

    df = df.fillna('null')
    G = nx.DiGraph()
    G.add_edges_from(zip(df['EmpID'],df['ManagerID'] ))
    G = nx.relabel_nodes(G, mapping=df.set_index('EmpID')['Name'].to_dict())
         df = df.merge(pd.DataFrame(nx.shortest_path(G)).T['null'], left_on='Name', right_index=True)
      # new column:
    df['Path'] = df['null'].apply(lambda x: '/'.join(x[-2::-1]) )

I keep getting this error.

df['Path'] = df['null'].apply(lambda x: '/'.join(x[-2::-1]) )
TypeError: 'float' object is not subscriptable

I have converted my excel columns to number so unsure what is causing the error.

ANswer: Found the answer to the issue actually, some of the rows had the same employee ID and Manager ID, hence the logic of looping wasn't working fine and the null column had no value. AFtex fixing that result is displaying fine.

Happy to accept answers of anyone who provides a solution for looping through and getting the results as specified by me in a different way.

misguided
  • 3.7k
  • 22
  • 54
  • 96