-1

enter image description here Reproduceable dataframe

df=pd.DataFrame({'(Source,Target,Weight)':['Majed Moqed,Majed Moqed,0']}) How do I split so that each comma separated element in the columns becomes the column head to the corresponding comma separated element in the rows?

Expected output

Source       Target      Weight

Majed Moqed Majed Moqed 0

1 Answers1

0

Try this:

col = '(Source,Target,Weight)'
df = pd.DataFrame(df[col].str.split(',').tolist(), columns=col[1:-1].split(','))

Output:

>>> df
        Source       Target Weight
0  Majed Moqed  Majed Moqed      0

Even better, create the desired DataFrame directly from edgelist:

df = pd.DataFrame(edgelist, columns=['Source', 'Target', 'Weight'])