import pandas as pd
import numpy as np
Filename = r'F:\数据科学入门课件\territories.xlsx'
df = pd.DataFrame(pd.read_excel(Filename))
Filename = r'F:\数据科学入门课件\area_length_2.xlsx'
df2 = pd.DataFrame(pd.read_excel(Filename))
#读入第一列数据——LPoint
LPoint = df['left'].tolist()
#读入第二列数据——RPoint
RPoint = df['right'].tolist()
#读入边长
Len = df['length'].tolist()
Len = [float(x) for x in Len]
#生成边的信息
LP=zip(LPoint,RPoint)
LP_list = [x for x in LP]
LP_list = list(set(LP_list))
#生成节点信息
df_nodes = df2['country']
Nodes = list(df_nodes)
#读取国家边界长度
df_clength = df2['Clength']
CLength = list(df_clength)
#读取国家面积
df_area = df2['area']
Area = list(df_area)
import networkx as nx
borders = nx.Graph()
#for node in Nodes:
# borders.add_node(node)
borders.add_nodes_from(Nodes)
#borders.nodes()
#borders.number_of_nodes()
#for item in LP_list:
# borders.add_edge(item[1],item[0])
borders.add_edges_from(LP_list)
#borders.edges()
#borders.number_of_edges()
#添加边属性,为相邻国家的距离
for item in LP_list:
x = item[0]
y = item[1]
ind = LP_list.index(item)
borders[x][y]['weight']= Len[ind]
#添加节点属性,每个国家的面积
for n in Nodes:
ind_n = Nodes.index(n)
borders.node[n]['area'] = Area[ind_n]
borders.node[n]['L'] = CLength[ind_n]
python boders数据读取
最新推荐文章于 2023-07-14 17:35:19 发布