Adjacency list to matrix pandas2019 Community Moderator ElectionHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow to make a flat list out of list of lists?How do I list all files of a directory?Renaming columns in pandasDelete column from pandas DataFrame by column nameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers

Ban on all campaign finance?

How could a female member of a species produce eggs unto death?

The use of "touch" and "touch on" in context

Why are the outputs of printf and std::cout different

PTIJ: Who should pay for Uber rides: the child or the parent?

Bastion server: use TCP forwarding VS placing private key on server

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

How to generate globally unique ids for different tables of the same database?

How is the Swiss post e-voting system supposed to work, and how was it wrong?

Why do Australian milk farmers need to protest supermarkets' milk price?

Could the Saturn V actually have launched astronauts around Venus?

How do anti-virus programs start at Windows boot?

Why are there 40 737 Max planes in flight when they have been grounded as not airworthy?

Making a sword in the stone, in a medieval world without magic

How do I hide Chekhov's Gun?

How to deal with a cynical class?

My adviser wants to be the first author

How to deal with taxi scam when on vacation?

Russian cases: A few examples, I'm really confused

Where is the 1/8 CR apprentice in Volo's Guide to Monsters?

Using "wallow" verb with object

What is the greatest age difference between a married couple in Tanach?

How to explain that I do not want to visit a country due to personal safety concern?

Font with correct density?



Adjacency list to matrix pandas



2019 Community Moderator ElectionHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow to make a flat list out of list of lists?How do I list all files of a directory?Renaming columns in pandasDelete column from pandas DataFrame by column nameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers










3















I'm trying to get through a toy example of building an adjacency matrix from a list, but already I can't quite figure it out. I am thinking in terms of .loc() but I'm not sure how to index correctly.



'nodes':['A', 'B', 'C', 'D', 'E'],
'edges':[('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
('D', 'E'), ('E', 'A'),('E', 'B'), ('E', 'C')]


I've started to build the matrix with:



n = len(graph['nodes'])
adj_matr = pd.DataFrame(0, columns = graph['nodes'], index = graph['edges'])


but now I'm not sure how to fill it in. I think there's an easy one liner, maybe with a list comprehension?



Expected output:



 A B C D E
A 0 1 0 1 0
B 0 0 1 0 1
C 0 0 0 1 0
D 0 0 0 0 1
E 1 1 1 0 0









share|improve this question



















  • 1





    What's the expected output?

    – pistol2myhead
    Mar 7 at 12:12











  • thank you, I added it now. Currently the dataframe/matrix is filled in with all 0s

    – Frederic Bastiat
    Mar 7 at 12:16











  • Updated the answer @FredericBastiat , as it seems from your adjacency matrix that the graph is directed

    – yatu
    Mar 7 at 15:51
















3















I'm trying to get through a toy example of building an adjacency matrix from a list, but already I can't quite figure it out. I am thinking in terms of .loc() but I'm not sure how to index correctly.



'nodes':['A', 'B', 'C', 'D', 'E'],
'edges':[('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
('D', 'E'), ('E', 'A'),('E', 'B'), ('E', 'C')]


I've started to build the matrix with:



n = len(graph['nodes'])
adj_matr = pd.DataFrame(0, columns = graph['nodes'], index = graph['edges'])


but now I'm not sure how to fill it in. I think there's an easy one liner, maybe with a list comprehension?



Expected output:



 A B C D E
A 0 1 0 1 0
B 0 0 1 0 1
C 0 0 0 1 0
D 0 0 0 0 1
E 1 1 1 0 0









share|improve this question



















  • 1





    What's the expected output?

    – pistol2myhead
    Mar 7 at 12:12











  • thank you, I added it now. Currently the dataframe/matrix is filled in with all 0s

    – Frederic Bastiat
    Mar 7 at 12:16











  • Updated the answer @FredericBastiat , as it seems from your adjacency matrix that the graph is directed

    – yatu
    Mar 7 at 15:51














3












3








3








I'm trying to get through a toy example of building an adjacency matrix from a list, but already I can't quite figure it out. I am thinking in terms of .loc() but I'm not sure how to index correctly.



'nodes':['A', 'B', 'C', 'D', 'E'],
'edges':[('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
('D', 'E'), ('E', 'A'),('E', 'B'), ('E', 'C')]


I've started to build the matrix with:



n = len(graph['nodes'])
adj_matr = pd.DataFrame(0, columns = graph['nodes'], index = graph['edges'])


but now I'm not sure how to fill it in. I think there's an easy one liner, maybe with a list comprehension?



Expected output:



 A B C D E
A 0 1 0 1 0
B 0 0 1 0 1
C 0 0 0 1 0
D 0 0 0 0 1
E 1 1 1 0 0









share|improve this question
















I'm trying to get through a toy example of building an adjacency matrix from a list, but already I can't quite figure it out. I am thinking in terms of .loc() but I'm not sure how to index correctly.



'nodes':['A', 'B', 'C', 'D', 'E'],
'edges':[('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
('D', 'E'), ('E', 'A'),('E', 'B'), ('E', 'C')]


I've started to build the matrix with:



n = len(graph['nodes'])
adj_matr = pd.DataFrame(0, columns = graph['nodes'], index = graph['edges'])


but now I'm not sure how to fill it in. I think there's an easy one liner, maybe with a list comprehension?



Expected output:



 A B C D E
A 0 1 0 1 0
B 0 0 1 0 1
C 0 0 0 1 0
D 0 0 0 0 1
E 1 1 1 0 0






python pandas dataframe adjacency-matrix adjacency-list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 12:17







Frederic Bastiat

















asked Mar 7 at 12:11









Frederic BastiatFrederic Bastiat

375317




375317







  • 1





    What's the expected output?

    – pistol2myhead
    Mar 7 at 12:12











  • thank you, I added it now. Currently the dataframe/matrix is filled in with all 0s

    – Frederic Bastiat
    Mar 7 at 12:16











  • Updated the answer @FredericBastiat , as it seems from your adjacency matrix that the graph is directed

    – yatu
    Mar 7 at 15:51













  • 1





    What's the expected output?

    – pistol2myhead
    Mar 7 at 12:12











  • thank you, I added it now. Currently the dataframe/matrix is filled in with all 0s

    – Frederic Bastiat
    Mar 7 at 12:16











  • Updated the answer @FredericBastiat , as it seems from your adjacency matrix that the graph is directed

    – yatu
    Mar 7 at 15:51








1




1





What's the expected output?

– pistol2myhead
Mar 7 at 12:12





What's the expected output?

– pistol2myhead
Mar 7 at 12:12













thank you, I added it now. Currently the dataframe/matrix is filled in with all 0s

– Frederic Bastiat
Mar 7 at 12:16





thank you, I added it now. Currently the dataframe/matrix is filled in with all 0s

– Frederic Bastiat
Mar 7 at 12:16













Updated the answer @FredericBastiat , as it seems from your adjacency matrix that the graph is directed

– yatu
Mar 7 at 15:51






Updated the answer @FredericBastiat , as it seems from your adjacency matrix that the graph is directed

– yatu
Mar 7 at 15:51













3 Answers
3






active

oldest

votes


















2














A simple way to obtain the adjacency matrix is by using NetworkX



d = 'nodes':['A', 'B', 'C', 'D', 'E'],
'edges':[('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
('D', 'E'), ('E', 'A'),('E', 'B'), ('E', 'C')]


It appears that from your adjacency matrix the graph is directed. You can create a directed graph as shown bellow and define its nodes and edges from the dictionary with:



import networkx as nx
g = nx.DiGraph()
g.add_nodes_from(d['nodes'])
g.add_edges_from(d['edges'])


And then you can obtain the adjacency matrix from the network with nx.adjacency_matrix:



m = nx.adjacency_matrix(g)
m.todense()

matrix([[0, 1, 0, 1, 0],
[0, 0, 1, 0, 1],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1],
[1, 1, 1, 0, 0]], dtype=int64)


And for a dataframe with the corresponding nodes as columns you can do:



pd.DataFrame(m.todense(), columns=nx.nodes(g))

A B C D E
0 0 1 0 1 0
1 0 0 1 0 1
2 0 0 0 1 0
3 0 0 0 0 1
4 1 1 1 0 0





share|improve this answer
































    1














    for undirected graph



    graph = 'nodes': ['A', 'B', 'C', 'D', 'E'],
    'edges': [('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
    ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C')]
    n = len(graph['nodes'])
    adj_matr = pd.DataFrame(0, columns=graph['nodes'], index=graph['nodes'])
    for i in graph['edges']:
    adj_matr.at[i[0], i[1]] = 1
    adj_matr.at[i[1], i[0]] = 1


    print(adj_matr)

    A B C D E
    A 0 1 0 1 1
    B 1 0 1 0 1
    C 0 1 0 1 1
    D 1 0 1 0 1
    E 1 1 1 1 0


    for directed graph:



    graph = 'nodes': ['A', 'B', 'C', 'D', 'E'],
    'edges': [('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
    ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C')]
    n = len(graph['nodes'])
    adj_matr = pd.DataFrame(0, columns=graph['nodes'], index=graph['nodes'])
    print(adj_matr)
    for i in graph['edges']:
    adj_matr.at[i[0], i[1]] = 1
    # adj_matr.at[i[1], i[0]] = 1

    print(adj_matr)

    A B C D E
    A 0 1 0 1 0
    B 0 0 1 0 1
    C 0 0 0 1 0
    D 0 0 0 0 1
    E 1 1 1 0 0





    share|improve this answer
































      0














      For a directed graph you can use:



      df = pd.DataFrame(graph['edges'], columns=['From', 'To'])
      df['Edge'] = 1
      adj = df.pivot(index='From', columns='To', values='Edge').fillna(0)





      share|improve this answer
























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55043492%2fadjacency-list-to-matrix-pandas%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        A simple way to obtain the adjacency matrix is by using NetworkX



        d = 'nodes':['A', 'B', 'C', 'D', 'E'],
        'edges':[('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
        ('D', 'E'), ('E', 'A'),('E', 'B'), ('E', 'C')]


        It appears that from your adjacency matrix the graph is directed. You can create a directed graph as shown bellow and define its nodes and edges from the dictionary with:



        import networkx as nx
        g = nx.DiGraph()
        g.add_nodes_from(d['nodes'])
        g.add_edges_from(d['edges'])


        And then you can obtain the adjacency matrix from the network with nx.adjacency_matrix:



        m = nx.adjacency_matrix(g)
        m.todense()

        matrix([[0, 1, 0, 1, 0],
        [0, 0, 1, 0, 1],
        [0, 0, 0, 1, 0],
        [0, 0, 0, 0, 1],
        [1, 1, 1, 0, 0]], dtype=int64)


        And for a dataframe with the corresponding nodes as columns you can do:



        pd.DataFrame(m.todense(), columns=nx.nodes(g))

        A B C D E
        0 0 1 0 1 0
        1 0 0 1 0 1
        2 0 0 0 1 0
        3 0 0 0 0 1
        4 1 1 1 0 0





        share|improve this answer





























          2














          A simple way to obtain the adjacency matrix is by using NetworkX



          d = 'nodes':['A', 'B', 'C', 'D', 'E'],
          'edges':[('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
          ('D', 'E'), ('E', 'A'),('E', 'B'), ('E', 'C')]


          It appears that from your adjacency matrix the graph is directed. You can create a directed graph as shown bellow and define its nodes and edges from the dictionary with:



          import networkx as nx
          g = nx.DiGraph()
          g.add_nodes_from(d['nodes'])
          g.add_edges_from(d['edges'])


          And then you can obtain the adjacency matrix from the network with nx.adjacency_matrix:



          m = nx.adjacency_matrix(g)
          m.todense()

          matrix([[0, 1, 0, 1, 0],
          [0, 0, 1, 0, 1],
          [0, 0, 0, 1, 0],
          [0, 0, 0, 0, 1],
          [1, 1, 1, 0, 0]], dtype=int64)


          And for a dataframe with the corresponding nodes as columns you can do:



          pd.DataFrame(m.todense(), columns=nx.nodes(g))

          A B C D E
          0 0 1 0 1 0
          1 0 0 1 0 1
          2 0 0 0 1 0
          3 0 0 0 0 1
          4 1 1 1 0 0





          share|improve this answer



























            2












            2








            2







            A simple way to obtain the adjacency matrix is by using NetworkX



            d = 'nodes':['A', 'B', 'C', 'D', 'E'],
            'edges':[('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
            ('D', 'E'), ('E', 'A'),('E', 'B'), ('E', 'C')]


            It appears that from your adjacency matrix the graph is directed. You can create a directed graph as shown bellow and define its nodes and edges from the dictionary with:



            import networkx as nx
            g = nx.DiGraph()
            g.add_nodes_from(d['nodes'])
            g.add_edges_from(d['edges'])


            And then you can obtain the adjacency matrix from the network with nx.adjacency_matrix:



            m = nx.adjacency_matrix(g)
            m.todense()

            matrix([[0, 1, 0, 1, 0],
            [0, 0, 1, 0, 1],
            [0, 0, 0, 1, 0],
            [0, 0, 0, 0, 1],
            [1, 1, 1, 0, 0]], dtype=int64)


            And for a dataframe with the corresponding nodes as columns you can do:



            pd.DataFrame(m.todense(), columns=nx.nodes(g))

            A B C D E
            0 0 1 0 1 0
            1 0 0 1 0 1
            2 0 0 0 1 0
            3 0 0 0 0 1
            4 1 1 1 0 0





            share|improve this answer















            A simple way to obtain the adjacency matrix is by using NetworkX



            d = 'nodes':['A', 'B', 'C', 'D', 'E'],
            'edges':[('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
            ('D', 'E'), ('E', 'A'),('E', 'B'), ('E', 'C')]


            It appears that from your adjacency matrix the graph is directed. You can create a directed graph as shown bellow and define its nodes and edges from the dictionary with:



            import networkx as nx
            g = nx.DiGraph()
            g.add_nodes_from(d['nodes'])
            g.add_edges_from(d['edges'])


            And then you can obtain the adjacency matrix from the network with nx.adjacency_matrix:



            m = nx.adjacency_matrix(g)
            m.todense()

            matrix([[0, 1, 0, 1, 0],
            [0, 0, 1, 0, 1],
            [0, 0, 0, 1, 0],
            [0, 0, 0, 0, 1],
            [1, 1, 1, 0, 0]], dtype=int64)


            And for a dataframe with the corresponding nodes as columns you can do:



            pd.DataFrame(m.todense(), columns=nx.nodes(g))

            A B C D E
            0 0 1 0 1 0
            1 0 0 1 0 1
            2 0 0 0 1 0
            3 0 0 0 0 1
            4 1 1 1 0 0






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 7 at 15:49

























            answered Mar 7 at 12:17









            yatuyatu

            13.4k31341




            13.4k31341























                1














                for undirected graph



                graph = 'nodes': ['A', 'B', 'C', 'D', 'E'],
                'edges': [('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
                ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C')]
                n = len(graph['nodes'])
                adj_matr = pd.DataFrame(0, columns=graph['nodes'], index=graph['nodes'])
                for i in graph['edges']:
                adj_matr.at[i[0], i[1]] = 1
                adj_matr.at[i[1], i[0]] = 1


                print(adj_matr)

                A B C D E
                A 0 1 0 1 1
                B 1 0 1 0 1
                C 0 1 0 1 1
                D 1 0 1 0 1
                E 1 1 1 1 0


                for directed graph:



                graph = 'nodes': ['A', 'B', 'C', 'D', 'E'],
                'edges': [('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
                ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C')]
                n = len(graph['nodes'])
                adj_matr = pd.DataFrame(0, columns=graph['nodes'], index=graph['nodes'])
                print(adj_matr)
                for i in graph['edges']:
                adj_matr.at[i[0], i[1]] = 1
                # adj_matr.at[i[1], i[0]] = 1

                print(adj_matr)

                A B C D E
                A 0 1 0 1 0
                B 0 0 1 0 1
                C 0 0 0 1 0
                D 0 0 0 0 1
                E 1 1 1 0 0





                share|improve this answer





























                  1














                  for undirected graph



                  graph = 'nodes': ['A', 'B', 'C', 'D', 'E'],
                  'edges': [('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
                  ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C')]
                  n = len(graph['nodes'])
                  adj_matr = pd.DataFrame(0, columns=graph['nodes'], index=graph['nodes'])
                  for i in graph['edges']:
                  adj_matr.at[i[0], i[1]] = 1
                  adj_matr.at[i[1], i[0]] = 1


                  print(adj_matr)

                  A B C D E
                  A 0 1 0 1 1
                  B 1 0 1 0 1
                  C 0 1 0 1 1
                  D 1 0 1 0 1
                  E 1 1 1 1 0


                  for directed graph:



                  graph = 'nodes': ['A', 'B', 'C', 'D', 'E'],
                  'edges': [('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
                  ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C')]
                  n = len(graph['nodes'])
                  adj_matr = pd.DataFrame(0, columns=graph['nodes'], index=graph['nodes'])
                  print(adj_matr)
                  for i in graph['edges']:
                  adj_matr.at[i[0], i[1]] = 1
                  # adj_matr.at[i[1], i[0]] = 1

                  print(adj_matr)

                  A B C D E
                  A 0 1 0 1 0
                  B 0 0 1 0 1
                  C 0 0 0 1 0
                  D 0 0 0 0 1
                  E 1 1 1 0 0





                  share|improve this answer



























                    1












                    1








                    1







                    for undirected graph



                    graph = 'nodes': ['A', 'B', 'C', 'D', 'E'],
                    'edges': [('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
                    ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C')]
                    n = len(graph['nodes'])
                    adj_matr = pd.DataFrame(0, columns=graph['nodes'], index=graph['nodes'])
                    for i in graph['edges']:
                    adj_matr.at[i[0], i[1]] = 1
                    adj_matr.at[i[1], i[0]] = 1


                    print(adj_matr)

                    A B C D E
                    A 0 1 0 1 1
                    B 1 0 1 0 1
                    C 0 1 0 1 1
                    D 1 0 1 0 1
                    E 1 1 1 1 0


                    for directed graph:



                    graph = 'nodes': ['A', 'B', 'C', 'D', 'E'],
                    'edges': [('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
                    ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C')]
                    n = len(graph['nodes'])
                    adj_matr = pd.DataFrame(0, columns=graph['nodes'], index=graph['nodes'])
                    print(adj_matr)
                    for i in graph['edges']:
                    adj_matr.at[i[0], i[1]] = 1
                    # adj_matr.at[i[1], i[0]] = 1

                    print(adj_matr)

                    A B C D E
                    A 0 1 0 1 0
                    B 0 0 1 0 1
                    C 0 0 0 1 0
                    D 0 0 0 0 1
                    E 1 1 1 0 0





                    share|improve this answer















                    for undirected graph



                    graph = 'nodes': ['A', 'B', 'C', 'D', 'E'],
                    'edges': [('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
                    ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C')]
                    n = len(graph['nodes'])
                    adj_matr = pd.DataFrame(0, columns=graph['nodes'], index=graph['nodes'])
                    for i in graph['edges']:
                    adj_matr.at[i[0], i[1]] = 1
                    adj_matr.at[i[1], i[0]] = 1


                    print(adj_matr)

                    A B C D E
                    A 0 1 0 1 1
                    B 1 0 1 0 1
                    C 0 1 0 1 1
                    D 1 0 1 0 1
                    E 1 1 1 1 0


                    for directed graph:



                    graph = 'nodes': ['A', 'B', 'C', 'D', 'E'],
                    'edges': [('A', 'B'), ('A', 'D'), ('B', 'C'), ('B', 'E'), ('C', 'D'),
                    ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C')]
                    n = len(graph['nodes'])
                    adj_matr = pd.DataFrame(0, columns=graph['nodes'], index=graph['nodes'])
                    print(adj_matr)
                    for i in graph['edges']:
                    adj_matr.at[i[0], i[1]] = 1
                    # adj_matr.at[i[1], i[0]] = 1

                    print(adj_matr)

                    A B C D E
                    A 0 1 0 1 0
                    B 0 0 1 0 1
                    C 0 0 0 1 0
                    D 0 0 0 0 1
                    E 1 1 1 0 0






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 7 at 12:24

























                    answered Mar 7 at 12:16









                    NihalNihal

                    3,22261532




                    3,22261532





















                        0














                        For a directed graph you can use:



                        df = pd.DataFrame(graph['edges'], columns=['From', 'To'])
                        df['Edge'] = 1
                        adj = df.pivot(index='From', columns='To', values='Edge').fillna(0)





                        share|improve this answer





























                          0














                          For a directed graph you can use:



                          df = pd.DataFrame(graph['edges'], columns=['From', 'To'])
                          df['Edge'] = 1
                          adj = df.pivot(index='From', columns='To', values='Edge').fillna(0)





                          share|improve this answer



























                            0












                            0








                            0







                            For a directed graph you can use:



                            df = pd.DataFrame(graph['edges'], columns=['From', 'To'])
                            df['Edge'] = 1
                            adj = df.pivot(index='From', columns='To', values='Edge').fillna(0)





                            share|improve this answer















                            For a directed graph you can use:



                            df = pd.DataFrame(graph['edges'], columns=['From', 'To'])
                            df['Edge'] = 1
                            adj = df.pivot(index='From', columns='To', values='Edge').fillna(0)






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 7 at 13:37

























                            answered Mar 7 at 13:32









                            JoergVanAkenJoergVanAken

                            84167




                            84167



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55043492%2fadjacency-list-to-matrix-pandas%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

                                Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                                List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229