当前位置: 首页>>代码示例>>Python>>正文


Python networkx.from_pandas_edgelist方法代码示例

本文整理汇总了Python中networkx.from_pandas_edgelist方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.from_pandas_edgelist方法的具体用法?Python networkx.from_pandas_edgelist怎么用?Python networkx.from_pandas_edgelist使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkx的用法示例。


在下文中一共展示了networkx.from_pandas_edgelist方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_from_edgelist

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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()) 
开发者ID:aws-samples,项目名称:aws-kube-codesuite,代码行数:21,代码来源:test_convert_pandas.py

示例2: test_from_edgelist

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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()) 
开发者ID:holzschu,项目名称:Carnets,代码行数:21,代码来源:test_convert_pandas.py

示例3: test_from_edgelist_multi_attr

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:8,代码来源:test_convert_pandas.py

示例4: create_from_pd

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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 
开发者ID:shobeir,项目名称:GraphiPy,代码行数:32,代码来源:exportnx.py

示例5: tnet_to_nx

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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 
开发者ID:wiheto,项目名称:teneto,代码行数:12,代码来源:io.py

示例6: test_from_edgelist_all_attr

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:13,代码来源:test_convert_pandas.py

示例7: test_from_edgelist_multi_attr_incl_target

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:8,代码来源:test_convert_pandas.py

示例8: test_from_edgelist_multidigraph_and_edge_attr

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:30,代码来源:test_convert_pandas.py

示例9: test_from_edgelist_one_attr

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:8,代码来源:test_convert_pandas.py

示例10: test_from_edgelist_invalid_attr

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:7,代码来源:test_convert_pandas.py

示例11: test_from_edgelist_no_attr

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:8,代码来源:test_convert_pandas.py

示例12: test_roundtrip

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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) 
开发者ID:holzschu,项目名称:Carnets,代码行数:13,代码来源:test_convert_pandas.py

示例13: from_pandas_dataframe

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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) 
开发者ID:aws-samples,项目名称:aws-kube-codesuite,代码行数:9,代码来源:convert_matrix.py

示例14: test_from_edgelist_all_attr

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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) 
开发者ID:aws-samples,项目名称:aws-kube-codesuite,代码行数:16,代码来源:test_convert_pandas.py

示例15: test_from_edgelist_multidigraph_and_edge_attr

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import from_pandas_edgelist [as 别名]
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) 
开发者ID:aws-samples,项目名称:aws-kube-codesuite,代码行数:30,代码来源:test_convert_pandas.py


注:本文中的networkx.from_pandas_edgelist方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。