本文整理汇总了Python中networkx.to_pandas_edgelist方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.to_pandas_edgelist方法的具体用法?Python networkx.to_pandas_edgelist怎么用?Python networkx.to_pandas_edgelist使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.to_pandas_edgelist方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __load_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import to_pandas_edgelist [as 别名]
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
示例2: test_roundtrip
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import to_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)
示例3: GRAPH_2_EDGELIST_CSV
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import to_pandas_edgelist [as 别名]
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)
示例4: GRAPH_3_EDGELIST_CSV
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import to_pandas_edgelist [as 别名]
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)
示例5: to_pandas_edgelist
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import to_pandas_edgelist [as 别名]
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)
示例6: to_pandas_edgelist
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import to_pandas_edgelist [as 别名]
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)