本文整理匯總了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)