Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat a column value

I have input dataframe in below format

enter image description here

I want the output in below format

enter image description here

Input data for referance

import pandas as pd
dfno = pd.DataFrame({'Nodes':['A','B','C'], 'Connections': ['Za,Eb', 'Qa,Rb', 'La,Mb']})

I tried below code to convert each value of both rows into list and then adding to dataframe. But it did not work. Here character in connection columns are getting split.

for index, row in dfno.iterrows():
    node = str(row['Nodes'])
    connec = list(str(row['Connections']))
    print(node)
    print(connec)

How to do this?

like image 760
SoKu Avatar asked May 24 '26 05:05

SoKu


1 Answers

import pandas as pd
dfno = pd.DataFrame({'Nodes':['A','B','C'], 'Connections': ['Za,Eb', 'Qa,Rb', 'La,Mb']})

for index, row in dfno.iterrows():
    node = str(row['Nodes'])
    connec = str(row['Connections']).split(',')
    print(node)
    print(connec)
like image 64
Powerful Lee Avatar answered May 25 '26 19:05

Powerful Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!