![]() |
How can I rearrange df as the nodes index in pytorch geometric manner? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How can I rearrange df as the nodes index in pytorch geometric manner? (/thread-40456.html) |
How can I rearrange df as the nodes index in pytorch geometric manner? - uqlsmey - Jul-31-2023 How can I rearrange df as the nodes index in pytorch geometric manner? This is my code data = {'Source': ['Rainfall', 'SP2', 'SP2', 'Inflow','Rainfall','Inflow', 'Inflow', 'Inflow','SWT','SP1','SP1','SWD'], 'Target': ['SP1', 'Evp', 'Outflow', 'SP2','SWD','SWD', 'SP2','SP1','SP1','SP2','Evp','Loss']} df = pd.DataFrame(data) nodes = pd.concat([df['Source'], df['Target']]).unique() node_indices = {node: i for i, node in enumerate(nodes)} df['Source'] = df['Source'].map(node_indices) df['Target'] = df['Target'].map(node_indices)My goal is to generate the following outputs: Source Source Source Target Target Source Source Target Target 4 4 4 4 4 1 1 1 1 0 2 3 1 6 2 2 6 7 Label Rainfall Inflow SWT SP2 Evp Inflow Inflow Evp Outflow |