Skip to content Skip to sidebar Skip to footer

Convert Pandas Data Frame To A List Of Tuples Containing Ids And A Weight

I have a data frame (called df) which is currently formatted like so: 1 2 3 1 1 0.26 0.02 2 0.26 1 0.61 3 0.02 0.61 1 The IDs are connected by a

Solution 1:

Can you try:

# this s is what you are looking fors = df.where(df.index.values > df.columns.values[:,None]).stack().reset_index(name='weight')

# we can use dataframe directlyG = nx.from_pandas_edgelist(s,source='level_0',target='level_1', edge_attr='weight')

Or even simpler:

G = nx.from_pandas_adjacency(df)

Post a Comment for "Convert Pandas Data Frame To A List Of Tuples Containing Ids And A Weight"