How to write Hierarchical query in PYTHON

2024/11/19 9:37:36

The given input is like:

EMPLOYEE_ID NAME     MANAGER_ID101         A       10102         B       1110          C       111          D       11           E       nullEmployee     Cycle      LEVEL Path101         A         101/10/1102         B         102/11/110          C         10/111          D         11/11           E         1

It will be great if one can solve it using python "pandas" library. I am not sure if it can be achieved using pandas or not. Other solutions are also welcomed.

Answer

dictionary with EMPLOYEE_ID and MANAGER_ID:

dct = dict(zip(df.EMPLOYEE_ID.values, df.MANAGER_ID.values))

function to create hierarchy string

def heirarchy(id):boss = str(id) + '/'while dct[id] != 'null':boss += dct[id] + '/'id = int(dct[id])return boss[:-1]

apply

df['LEVEL'] = df.EMPLOYEE_ID.apply(heirarchy)# ResultEMPLOYEE_ID NAME MANAGER_ID     LEVEL
0          101    A         10  101/10/1
1          102    B         11  102/11/1
2           10    C          1      10/1
3           11    D          1      11/1
4            1    E       null         1
https://en.xdnf.cn/q/118561.html

Related Q&A

Unable to launch selenium with python in mac

Im facing an issue with selenium with python in Mac OS.. Python 2.7 pydev 3.0My sample codefrom selenium import webdriver driver = webdriver.Firefox() driver.get("https://www.formsite.com/") …

Memory error In instantiating the numpy array

I have a list A of a 50,000 elements and each element is an array of shape (102400) I tried instantiating an array B.B=numpy.array(A)But this throws an exception MemoryError.I know that the memory and …

Setting column names in a pandas dataframe (Python)

When setting a column name for a pandas dataframe, why does the following work:df_coeff = pd.DataFrame(data = lm.coef_, index = X.columns, columns = [Coefficient])While this does not workdf_coeff = pd.…

Check that Python function does not modify argument?

You know how in Python, if v is a list or a dictionary, its quite common to write functions that modify v in place (instead of just returning the new value). Im wondering if it is possible to write a c…

What Python 3 version for my Django project

I will try to port my Python 2.7 with Django to Python 3. But now my question is what version is the most stable one today? Ive heard people use 3.2 and 3.4 and recommend it. But now Im asking you guy…

Error during runfile in Eclipse with PyDev/ error initializing console

Using a PyDev console in Eclipse, which initially worked fine. Python code would work inside the console. When I started writing a file within a PyDev module, I tried executing runfile() but the consol…

Reversibly encode two large integers of different bit lengths into one integer

I want to encode two large integers of possibly different maximum bit lengths into a single integer. The first integer is signed (can be negative) whereas the second is unsigned (always non-negative). …

Pytube AttributeError: NoneType object has no attribute span

Hi I have a problem with AttributeError: NoneType object has no attribute span I read on the StackOverflow a channel with this problem on this I found the potential solution but it still not working he…

GDB: ModuleNotFoundError: No module named _tkinter [duplicate]

This question already has answers here:Why does tkinter (or turtle) seem to be missing or broken? Shouldnt it be part of the standard library?(4 answers)Closed last year.So Im trying to debug my C co…

Why can the difference of different floating numbers be 0 in python? [duplicate]

This question already has answers here:Float to Int type conversion in Python for large integers/numbers(2 answers)Closed last year.Why is the result of below code 0 in python3? a = "4.1512940685…