Python networkx.to_pandas_edgelist() Examples
The following are 7
code examples of networkx.to_pandas_edgelist().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: multicom.py From cdlib with BSD 2-Clause "Simplified" License | 6 votes |
def __load_graph(self): """ Load an undirected and unweighted graph from an edge-list file. :param edgelist_filename: string or unicode Path to the edge-list file. Id of nodes are assumed to be non-negative integers. :param delimiter: str, default '\t' :param comment: str, default '#' :return: Compressed Sparse Row Matrix Adjacency matrix of the graph """ edge_df = nx.to_pandas_edgelist(self.g) edge_list = edge_df.values n_nodes = int(np.max(edge_list) + 1) adj_matrix = sparse.coo_matrix((np.ones(edge_list.shape[0]), (edge_list[:, 0], edge_list[:, 1])), shape=tuple([n_nodes, n_nodes]), dtype=edge_list.dtype) adj_matrix = adj_matrix.tocsr() adj_matrix = adj_matrix + adj_matrix.T return adj_matrix
Example #2
Source File: test_convert_pandas.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_roundtrip(self): # edgelist Gtrue = nx.Graph([(1, 1), (1, 2)]) df = nx.to_pandas_edgelist(Gtrue) G = nx.from_pandas_edgelist(df) assert_graphs_equal(Gtrue, G) # adjacency Gtrue = nx.Graph(({1: {1: {'weight': 1}, 2: {'weight': 1}}, 2: {1: {'weight': 1}}})) df = nx.to_pandas_adjacency(Gtrue, dtype=int) G = nx.from_pandas_adjacency(df) assert_graphs_equal(Gtrue, G)
Example #3
Source File: test_convert_pandas.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_roundtrip(self): # edgelist Gtrue = nx.Graph([(1, 1), (1, 2)]) df = nx.to_pandas_edgelist(Gtrue) G = nx.from_pandas_edgelist(df) assert_graphs_equal(Gtrue, G) # adjacency Gtrue = nx.Graph(({1: {1: {'weight': 1}, 2: {'weight': 1}}, 2: {1: {'weight': 1}}})) df = nx.to_pandas_adjacency(Gtrue, dtype=int) G = nx.from_pandas_adjacency(df) assert_graphs_equal(Gtrue, G)
Example #4
Source File: conftest.py From postman_problems with MIT License | 5 votes |
def GRAPH_2_EDGELIST_CSV(GRAPH_2): edgelist = nx.to_pandas_edgelist(GRAPH_2, source='_node1', target='_node2') return create_mock_csv_from_dataframe(edgelist)
Example #5
Source File: conftest.py From postman_problems with MIT License | 5 votes |
def GRAPH_3_EDGELIST_CSV(GRAPH_3): edgelist = nx.to_pandas_edgelist(GRAPH_3, source='_node1', target='_node2') return create_mock_csv_from_dataframe(edgelist)
Example #6
Source File: convert_matrix.py From Carnets with BSD 3-Clause "New" or "Revised" License | 4 votes |
def to_pandas_edgelist(G, source='source', target='target', nodelist=None, dtype=None, order=None): """Returns the graph edge list as a Pandas DataFrame. Parameters ---------- G : graph The NetworkX graph used to construct the Pandas DataFrame. source : str or int, optional A valid column name (string or integer) for the source nodes (for the directed case). target : str or int, optional A valid column name (string or integer) for the target nodes (for the directed case). nodelist : list, optional Use only nodes specified in nodelist Returns ------- df : Pandas DataFrame Graph edge list Examples -------- >>> G = nx.Graph([('A', 'B', {'cost': 1, 'weight': 7}), ... ('C', 'E', {'cost': 9, 'weight': 10})]) >>> df = nx.to_pandas_edgelist(G, nodelist=['A', 'C']) >>> df[['source', 'target', 'cost', 'weight']] source target cost weight 0 A B 1 7 1 C E 9 10 """ import pandas as pd if nodelist is None: edgelist = G.edges(data=True) else: edgelist = G.edges(nodelist, data=True) source_nodes = [s for s, t, d in edgelist] target_nodes = [t for s, t, d in edgelist] all_keys = set().union(*(d.keys() for s, t, d in edgelist)) edge_attr = {k: [d.get(k, float("nan")) for s, t, d in edgelist] for k in all_keys} edgelistdict = {source: source_nodes, target: target_nodes} edgelistdict.update(edge_attr) return pd.DataFrame(edgelistdict)
Example #7
Source File: convert_matrix.py From aws-kube-codesuite with Apache License 2.0 | 4 votes |
def to_pandas_edgelist(G, source='source', target='target', nodelist=None, dtype=None, order=None): """Return the graph edge list as a Pandas DataFrame. Parameters ---------- G : graph The NetworkX graph used to construct the Pandas DataFrame. source : str or int, optional A valid column name (string or iteger) for the source nodes (for the directed case). target : str or int, optional A valid column name (string or iteger) for the target nodes (for the directed case). nodelist : list, optional Use only nodes specified in nodelist Returns ------- df : Pandas DataFrame Graph edge list Examples -------- >>> G = nx.Graph([('A', 'B', {'cost': 1, 'weight': 7}), ... ('C', 'E', {'cost': 9, 'weight': 10})]) >>> df = nx.to_pandas_edgelist(G, nodelist=['A', 'C']) >>> df cost source target weight 0 1 A B 7 1 9 C E 10 """ import pandas as pd if nodelist is None: edgelist = G.edges(data=True) else: edgelist = G.edges(nodelist, data=True) source_nodes = [s for s, t, d in edgelist] target_nodes = [t for s, t, d in edgelist] all_keys = set().union(*(d.keys() for s, t, d in edgelist)) edge_attr = {k: [d.get(k, float("nan")) for s, t, d in edgelist] for k in all_keys} edgelistdict = {source: source_nodes, target: target_nodes} edgelistdict.update(edge_attr) return pd.DataFrame(edgelistdict)