Python networkx.from_pandas_edgelist() Examples

The following are 21 code examples of networkx.from_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: test_convert_pandas.py    From aws-kube-codesuite with Apache License 2.0 6 votes vote down vote up
def test_from_edgelist(self):
        # Pandas DataFrame
        g = nx.cycle_graph(10)
        G = nx.Graph()
        G.add_nodes_from(g)
        G.add_weighted_edges_from((u, v, u) for u, v in g.edges())
        edgelist = nx.to_edgelist(G)
        source = [s for s, t, d in edgelist]
        target = [t for s, t, d in edgelist]
        weight = [d['weight'] for s, t, d in edgelist]
        edges = pd.DataFrame({'source': source,
                              'target': target,
                              'weight': weight})
        GG = nx.from_pandas_edgelist(edges, edge_attr='weight')
        assert_nodes_equal(G.nodes(), GG.nodes())
        assert_edges_equal(G.edges(), GG.edges())
        GW = nx.to_networkx_graph(edges, create_using=nx.Graph())
        assert_nodes_equal(G.nodes(), GW.nodes())
        assert_edges_equal(G.edges(), GW.edges()) 
Example #2
Source File: test_convert_pandas.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_from_edgelist_multidigraph_and_edge_attr(self):
        # example from issue #2374
        Gtrue = nx.MultiDiGraph([('X1', 'X4', {'Co': 'zA', 'Mi': 0, 'St': 'X1'}),
                                 ('X1', 'X4', {'Co': 'zB', 'Mi': 54, 'St': 'X2'}),
                                 ('X1', 'X4', {'Co': 'zB', 'Mi': 49, 'St': 'X3'}),
                                 ('X1', 'X4', {'Co': 'zB', 'Mi': 44, 'St': 'X4'}),
                                 ('Y1', 'Y3', {'Co': 'zC', 'Mi': 0, 'St': 'Y1'}),
                                 ('Y1', 'Y3', {'Co': 'zC', 'Mi': 34, 'St': 'Y2'}),
                                 ('Y1', 'Y3', {'Co': 'zC', 'Mi': 29, 'St': 'X2'}),
                                 ('Y1', 'Y3', {'Co': 'zC', 'Mi': 24, 'St': 'Y3'}),
                                 ('Z1', 'Z3', {'Co': 'zD', 'Mi': 0, 'St': 'Z1'}),
                                 ('Z1', 'Z3', {'Co': 'zD', 'Mi': 14, 'St': 'X3'}),
                                 ('Z1', 'Z3', {'Co': 'zE', 'Mi': 9, 'St': 'Z2'}),
                                 ('Z1', 'Z3', {'Co': 'zE', 'Mi': 4, 'St': 'Z3'})])
        df = pd.DataFrame.from_dict({
            'O': ['X1', 'X1', 'X1', 'X1', 'Y1', 'Y1', 'Y1', 'Y1', 'Z1', 'Z1', 'Z1', 'Z1'],
            'D': ['X4', 'X4', 'X4', 'X4', 'Y3', 'Y3', 'Y3', 'Y3', 'Z3', 'Z3', 'Z3', 'Z3'],
            'St': ['X1', 'X2', 'X3', 'X4', 'Y1', 'Y2', 'X2', 'Y3', 'Z1', 'X3', 'Z2', 'Z3'],
            'Co': ['zA', 'zB', 'zB', 'zB', 'zC', 'zC', 'zC', 'zC', 'zD', 'zD', 'zE', 'zE'],
            'Mi': [0,   54,   49,   44,    0,   34,   29,   24,    0,   14,    9,   4]})
        G1 = nx.from_pandas_edgelist(df, source='O', target='D',
                                     edge_attr=True,
                                     create_using=nx.MultiDiGraph)
        G2 = nx.from_pandas_edgelist(df, source='O', target='D',
                                     edge_attr=['St', 'Co', 'Mi'],
                                     create_using=nx.MultiDiGraph)
        assert_graphs_equal(G1, Gtrue)
        assert_graphs_equal(G2, Gtrue) 
Example #3
Source File: test_convert_pandas.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_from_edgelist(self):
        # Pandas DataFrame
        g = nx.cycle_graph(10)
        G = nx.Graph()
        G.add_nodes_from(g)
        G.add_weighted_edges_from((u, v, u) for u, v in g.edges())
        edgelist = nx.to_edgelist(G)
        source = [s for s, t, d in edgelist]
        target = [t for s, t, d in edgelist]
        weight = [d['weight'] for s, t, d in edgelist]
        edges = pd.DataFrame({'source': source,
                              'target': target,
                              'weight': weight})
        GG = nx.from_pandas_edgelist(edges, edge_attr='weight')
        assert_nodes_equal(G.nodes(), GG.nodes())
        assert_edges_equal(G.edges(), GG.edges())
        GW = nx.to_networkx_graph(edges, create_using=nx.Graph)
        assert_nodes_equal(G.nodes(), GW.nodes())
        assert_edges_equal(G.edges(), GW.edges()) 
Example #4
Source File: test_convert_pandas.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_from_edgelist_invalid_attr(self):
        assert_raises(nx.NetworkXError, nx.from_pandas_edgelist,
                      self.df, 0, 'b', 'misspell')
        assert_raises(nx.NetworkXError, nx.from_pandas_edgelist,
                      self.df, 0, 'b', 1) 
Example #5
Source File: test_convert_pandas.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_from_edgelist_one_attr(self):
        Gtrue = nx.Graph([('E', 'C', {'weight': 10}),
                          ('B', 'A', {'weight': 7}),
                          ('A', 'D', {'weight': 4})])
        G = nx.from_pandas_edgelist(self.df, 0, 'b', 'weight')
        assert_graphs_equal(G, Gtrue) 
Example #6
Source File: test_convert_pandas.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_from_edgelist_multidigraph_and_edge_attr(self):
        # example from issue #2374
        Gtrue = nx.MultiDiGraph([('X1', 'X4', {'Co': 'zA', 'Mi': 0, 'St': 'X1'}),
                                 ('X1', 'X4', {'Co': 'zB', 'Mi': 54, 'St': 'X2'}),
                                 ('X1', 'X4', {'Co': 'zB', 'Mi': 49, 'St': 'X3'}),
                                 ('X1', 'X4', {'Co': 'zB', 'Mi': 44, 'St': 'X4'}),
                                 ('Y1', 'Y3', {'Co': 'zC', 'Mi': 0, 'St': 'Y1'}),
                                 ('Y1', 'Y3', {'Co': 'zC', 'Mi': 34, 'St': 'Y2'}),
                                 ('Y1', 'Y3', {'Co': 'zC', 'Mi': 29, 'St': 'X2'}),
                                 ('Y1', 'Y3', {'Co': 'zC', 'Mi': 24, 'St': 'Y3'}),
                                 ('Z1', 'Z3', {'Co': 'zD', 'Mi': 0, 'St': 'Z1'}),
                                 ('Z1', 'Z3', {'Co': 'zD', 'Mi': 14, 'St': 'X3'}),
                                 ('Z1', 'Z3', {'Co': 'zE', 'Mi': 9, 'St': 'Z2'}),
                                 ('Z1', 'Z3', {'Co': 'zE', 'Mi': 4, 'St': 'Z3'})])
        df = pd.DataFrame.from_items([
                ('O', ['X1', 'X1', 'X1', 'X1', 'Y1', 'Y1', 'Y1', 'Y1', 'Z1', 'Z1', 'Z1', 'Z1']),
                ('D', ['X4', 'X4', 'X4', 'X4', 'Y3', 'Y3', 'Y3', 'Y3', 'Z3', 'Z3', 'Z3', 'Z3']),
                ('St', ['X1', 'X2', 'X3', 'X4', 'Y1', 'Y2', 'X2', 'Y3', 'Z1', 'X3', 'Z2', 'Z3']),
                ('Co', ['zA', 'zB', 'zB', 'zB', 'zC', 'zC', 'zC', 'zC', 'zD', 'zD', 'zE', 'zE']),
                ('Mi', [0,   54,   49,   44,    0,   34,   29,   24,    0,   14,    9,   4])])
        G1 = nx.from_pandas_edgelist(df, source='O', target='D',
                                     edge_attr=True,
                                     create_using=nx.MultiDiGraph())
        G2 = nx.from_pandas_edgelist(df, source='O', target='D',
                                     edge_attr=['St', 'Co', 'Mi'],
                                     create_using=nx.MultiDiGraph())
        assert_graphs_equal(G1, Gtrue)
        assert_graphs_equal(G2, Gtrue) 
Example #7
Source File: test_convert_pandas.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_from_edgelist_multi_attr(self):
        Gtrue = nx.Graph([('E', 'C', {'cost': 9, 'weight': 10}),
                          ('B', 'A', {'cost': 1, 'weight': 7}),
                          ('A', 'D', {'cost': 7, 'weight': 4})])
        G = nx.from_pandas_edgelist(self.df, 0, 'b', ['weight', 'cost'])
        assert_graphs_equal(G, Gtrue) 
Example #8
Source File: test_convert_pandas.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_from_edgelist_all_attr(self):
        Gtrue = nx.Graph([('E', 'C', {'cost': 9, 'weight': 10}),
                          ('B', 'A', {'cost': 1, 'weight': 7}),
                          ('A', 'D', {'cost': 7, 'weight': 4})])
        G = nx.from_pandas_edgelist(self.df, 0, 'b', True)
        assert_graphs_equal(G, Gtrue)
        # deprecated
        G = nx.from_pandas_dataframe(self.df, 0, 'b', True)
        assert_graphs_equal(G, Gtrue)
        # MultiGraph
        MGtrue = nx.MultiGraph(Gtrue)
        MGtrue.add_edge('A', 'D', cost=16, weight=4)
        MG = nx.from_pandas_edgelist(self.mdf, 0, 'b', True, nx.MultiGraph())
        assert_graphs_equal(MG, MGtrue) 
Example #9
Source File: convert_matrix.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def from_pandas_dataframe(df, source='source', target='target', edge_attr=None,
                          create_using=None):
    """DEPRECATED: Replaced by ``from_pandas_edgelist``."""
    msg = "from_pandas_dataframe is deprecated and will be removed" \
        "in 2.1, use from_pandas_edgelist instead."
    _warnings.warn(msg, DeprecationWarning)
    return from_pandas_edgelist(df, source, target, edge_attr, create_using) 
Example #10
Source File: test_convert_pandas.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 #11
Source File: test_convert_pandas.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_from_edgelist_no_attr(self):
        Gtrue = nx.Graph([('E', 'C', {}),
                          ('B', 'A', {}),
                          ('A', 'D', {})])
        G = nx.from_pandas_edgelist(self.df, 0, 'b',)
        assert_graphs_equal(G, Gtrue) 
Example #12
Source File: test_convert_pandas.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_from_edgelist_one_attr(self):
        Gtrue = nx.Graph([('E', 'C', {'weight': 10}),
                          ('B', 'A', {'weight': 7}),
                          ('A', 'D', {'weight': 4})])
        G = nx.from_pandas_edgelist(self.df, 0, 'b', 'weight')
        assert_graphs_equal(G, Gtrue) 
Example #13
Source File: test_convert_pandas.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_from_edgelist_multi_attr_incl_target(self):
        Gtrue = nx.Graph([('E', 'C', {0: 'C', 'b': 'E', 'weight': 10}),
                          ('B', 'A', {0: 'B', 'b': 'A', 'weight': 7}),
                          ('A', 'D', {0: 'A', 'b': 'D', 'weight': 4})])
        G = nx.from_pandas_edgelist(self.df, 0, 'b', [0, 'b', 'weight'])
        assert_graphs_equal(G, Gtrue) 
Example #14
Source File: test_convert_pandas.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_from_edgelist_multi_attr(self):
        Gtrue = nx.Graph([('E', 'C', {'cost': 9, 'weight': 10}),
                          ('B', 'A', {'cost': 1, 'weight': 7}),
                          ('A', 'D', {'cost': 7, 'weight': 4})])
        G = nx.from_pandas_edgelist(self.df, 0, 'b', ['weight', 'cost'])
        assert_graphs_equal(G, Gtrue) 
Example #15
Source File: test_convert_pandas.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_from_edgelist_all_attr(self):
        Gtrue = nx.Graph([('E', 'C', {'cost': 9, 'weight': 10}),
                          ('B', 'A', {'cost': 1, 'weight': 7}),
                          ('A', 'D', {'cost': 7, 'weight': 4})])
        G = nx.from_pandas_edgelist(self.df, 0, 'b', True)
        assert_graphs_equal(G, Gtrue)
        # MultiGraph
        MGtrue = nx.MultiGraph(Gtrue)
        MGtrue.add_edge('A', 'D', cost=16, weight=4)
        MG = nx.from_pandas_edgelist(self.mdf, 0, 'b', True, nx.MultiGraph())
        assert_graphs_equal(MG, MGtrue) 
Example #16
Source File: test_convert_pandas.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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 #17
Source File: io.py    From teneto with GNU General Public License v3.0 5 votes vote down vote up
def tnet_to_nx(df, t=None):
    """Creates undirected networkx object"""
    if t is not None:
        df = get_network_when(df, t=t)
    if 'weight' in df.columns:
        nxobj = nx.from_pandas_edgelist(
            df, source='i', target='j', edge_attr='weight')
    else:
        nxobj = nx.from_pandas_edgelist(df, source='i', target='j')
    return nxobj 
Example #18
Source File: exportnx.py    From GraphiPy with MIT License 5 votes vote down vote up
def create_from_pd(self, pd_graph, nx_graph=None, directional=False):

        nodes_df = pd_graph.get_nodes()
        edges_df = pd_graph.get_edges()

        # Create graph from edgelist dataframes
        if nx_graph is None:
            if directional:
                nx_graph = nx.DiGraph()
            else:
                nx_graph = nx.Graph()

        for key in edges_df:
            new_graph = nx.from_pandas_edgelist(
                edges_df[key], source="Source", target="Target", edge_attr=True)

            nx_graph = nx.compose(nx_graph, new_graph)

        # Add node attributes
        for key in nodes_df:
            df = nodes_df[key]

            for index, row in df.iterrows():
                _id = row["Id"]
                node = nx_graph.node[_id]

                for attr in row.keys():
                    node[attr] = row[attr]

        return nx_graph 
Example #19
Source File: search.py    From hoaxy-backend with GNU General Public License v3.0 4 votes vote down vote up
def limit_by_k_core(df, nodes_limit, edges_limit):
    """Use k_core method to remove less import nodes and edges.

    Parameters
    ----------
    df : pandas.DataFrame
        The edges dataframe.
    nodes_limit : int
        The maximum number of nodes to return.
    edges_limit : int
        The maximum number of edges to return.

    Returns
    -------
    pandas.DataFrame
        This dataframe is refined with k_core algorithm.
    """
    v_cols = ['from_user_id', 'to_user_id']
    G = nx.from_pandas_edgelist(
        df, v_cols[0], v_cols[1], create_using=nx.DiGraph())
    G.remove_edges_from(G.selfloop_edges())
    #
    # sort nodes by ascending core number
    core = nx.core_number(G)
    nodes_list = sorted(list(core.items()), key=lambda k: k[1], reverse=False)
    nodes_list = list(zip(*nodes_list))[0]
    nodes_list = list(nodes_list)
    #
    # if there are no nodes in excess, do not execute
    excess_nodes = G.number_of_nodes() - nodes_limit
    if nodes_limit and excess_nodes > 0:
        nodes_to_remove = nodes_list[:excess_nodes]
        nodes_list = nodes_list[excess_nodes:]
        G.remove_nodes_from(nodes_to_remove)
    #
    # remove nodes in batches until the the number of edges is below the
    # limit. Only execute if edges_limit argument is passed (not None) and
    # is positive
    if edges_limit:
        batch_size = 10
        while G.number_of_edges() > edges_limit:
            nodes_to_remove = nodes_list[:batch_size]
            nodes_list = nodes_list[batch_size:]
            G.remove_nodes_from(nodes_to_remove)
    logger.debug('filtered nodes/edges = %s/%s',
                 G.number_of_nodes(), G.number_of_edges())
    df = df.set_index(['from_user_id', 'to_user_id'])
    df = df.loc[list(G.edges())]
    return df.reset_index() 
Example #20
Source File: graph_mixin.py    From FlowKit with Mozilla Public License 2.0 4 votes vote down vote up
def to_networkx(self, source=None, target=None, directed_graph=True):
        """
        By default, the leftmost column will be used as the source, the next
        leftmost as the target, and any other columns will become edge attributes.
        Both or neither of source and target should be provided. 
        
        The default is to return a directed graph.

        Parameters
        ----------
        source : str, optional
            Optionally specify the column name for the source nodes.
        target : str, optional
            Optionally specify the column name for the target nodes.
        directed_graph : bool, default True
            Set to false to return an undirected graph.

        Returns
        -------
        networkx.Graph
            This query as a networkx graph object.

        """

        if bool(source) != bool(target):
            raise ValueError(
                "Both source and target must be specified, " + "or neither should."
            )

        if not source:
            source, target = self.column_names[:2]

        df = self.get_dataframe()
        if directed_graph:
            if (df.groupby([source, target])[source].count() - 1).any():
                warnings.warn(
                    " Duplicate edges in directed graph. Edge "
                    "information will be lost.",
                    stacklevel=2,
                )

            g_type = nx.DiGraph()

        else:
            df["links"] = [tuple(sorted(x)) for x in zip(df[source], df[target])]
            if (df.groupby(["links"])[source].count() - 1).any():
                warnings.warn(
                    " Duplicate edges in undirected graph. Edge "
                    "information will be lost.",
                    stacklevel=2,
                )

            del df["links"]
            g_type = nx.Graph()

        return nx.from_pandas_edgelist(
            df, source, target, edge_attr=True, create_using=g_type
        ) 
Example #21
Source File: d3fdgraph.py    From d3fdgraph with GNU General Public License v2.0 4 votes vote down vote up
def plot_force_directed_graph(node1_node1_weight, node_radius=3, link_distance=20, collision_scale=4, link_width_scale=4):

    # column names for node source and target, and edge attributes
    node_source_name = node1_node1_weight.columns.values[0]
    node_target_name = node1_node1_weight.columns.values[1]
    link_edge_name = node1_node1_weight.columns.values[2]

    # convert node1_node1_weight to graph
    graph = networkx.from_pandas_edgelist(node1_node1_weight, source=node_source_name, target=node_target_name, edge_attr=link_edge_name)

    # convert graph nodes and inks to json, ready for d3
    graph_json = networkx.readwrite.json_graph.node_link_data(graph)
    graph_json_nodes = graph_json['nodes']
    graph_json_links = graph_json['links']

    
    # load html and javascript from template files
    resource_package = __name__ 
    html_template = 'd3fdgraph.html'
    html = pkg_resources.resource_string(resource_package, html_template).decode('utf-8')
    javascript_template = 'd3fdgraph.js'
    js_code = pkg_resources.resource_string(resource_package, javascript_template).decode('utf-8')

    # generate random identifier for SVG element, to avoid name clashes if used multiple times in a notebook
    random_id_string = str(random.randrange(1000000,9999999))
    # replace placeholder in both html and js templates
    html = html.replace('%%unique-id%%', random_id_string)
    js_code = js_code.replace('%%unique-id%%', random_id_string)

    # substitute configuration values
    js_code = js_code.replace('%%noderadius%%', str(node_radius))
    js_code = js_code.replace('%%linkdistance%%', str(link_distance))
    js_code = js_code.replace('%%collisionscale%%', str(collision_scale))
    js_code = js_code.replace('%%linkwidthscale%%', str(link_width_scale))

    # substitute links and data
    js_code = js_code.replace('%%links%%', str(graph_json_links))
    js_code = js_code.replace('%%nodes%%', str(graph_json_nodes))
    js_code = js_code.replace('%%edge_attribute%%', link_edge_name)

    # display html in notebook cell
    IPython.core.display.display_html(IPython.core.display.HTML(html))

    # display (run) javascript in notebook cell
    IPython.core.display.display_javascript(IPython.core.display.Javascript(data=js_code))
    pass