简体   繁体   English

从CSV文件读取图形时出现问题。 无法读取CSV文件上的标题

[英]Problem on reading making a graph from a csv file. Won't read the header on the CSV file

I am new to Python and have been following this tutorial online that I found on solving the Chinese postman problem. 我是Python的新手,并且一直在网上关注本教程,该教程是我在解决中文邮递员问题时发现的。

I've uploaded the CSV files that I needed, but whenever I am trying to define node positions data structure for plotting, it keeps saying '' KeyError: 'X' '' which is one of the headers on my csv files 我已经上传了所需的CSV文件,但是每当我尝试定义用于绘制的节点位置数据结构时,它都会一直说“ KeyError:'X'”,这是我的csv文件的头之一

I've been told by a friend that a possible problem could be that there were spaces in the CSV files, but I am unsure how to fix this as well. 一位朋友告诉我,一个可能的问题可能是CSV文件中存在空格,但是我不确定如何解决此问题。

I've tried using the files that was from the tutorial online and it was working fine so I am unsure of what I've done wrong. 我已经尝试过使用在线教程中的文件,并且工作正常,所以我不确定自己做错了什么。

ID, X, Y
'rep1', 1, 1811
etc..

is the kind of output I get when print(df) runs 是print(df)运行时得到的输出类型

I have also tried using delimeter but I may have been doing it wrong. 我也尝试过使用定界符,但可能做错了。

import itertools
import copy
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt


df1 = pd.read_csv("U:\directory\edge_list_3.csv")
df1=df1.dropna()
print(df1)

#add the r before the directory for it to be read
import pandas as pd
df = pd.read_csv(r"U:\\directory\nodes.csv")
df=df.dropna()
print(df)

##trial no 2, attempting to clear the white spaces
df=pd.read_csv(r'U:\\directory\nodes.csv', delim_whitespace=True)
data= pd.read_csv(r'U:\\directory\nodes.csv, error_bad_lines=False)
df=pd.read_csv(r'U:\\directory\nodes.csv', delimeter='  ')


#############################
#creating an empty graph 
g= nx.Graph()


df1_dict = df1.to_dict()

#display
df1_dict
###########Return the copied line here###
import networkx as nx
g=nx.Graph()


for i, elrow in df1.iterrows():

    g.add_edge(elrow[0], elrow[1], **elrow[2:].to_dict())

print('\n')
    #edge list check
print(elrow[0])
print('\n')
print(elrow[1]) 

print(elrow[2:].to_dict())
################################
#node to a dict
df_dict=df.to_dict()

#Adding the node attributes

for i, nlrow in df.iterrows():
    nx.set_node_attributes(g, {nlrow['ID']: nlrow[1:].to_dict()})


    #Node list
    print(nlrow)


    #preview of the first  5 edges
list(g.edges(data=True))[0:5]

#same for nodes
list(g.nodes(data=True))[0:10]

print('no of edges: {}'.format(g.number_of_edges()))
print('no of nodes: {}'.format(g.number_of_nodes()))


#Problematic line
# Define node positions data structure (dict) for plotting
node_positions = {node[0]: (node[1]['X'], -node[1]['Y']) for node in g.nodes(data=True)}

  File "<ipython-input-22-3dbf80b62cb7>", line 19
    df=pd.read_csv(\\r'U:\\GE90\\nodes.csv', delim_whitespace=True)
                                                                   ^
SyntaxError: unexpected character after line continuation character

# and if I take off the delimiter lines: #并且如果我取消分隔线:


KeyError                                  Traceback (most recent call last)
<ipython-input-23-38d6675d5393> in <module>
     79 
     80 # Define node positions data structure (dict) for plotting
---> 81 node_positions = {node[0]: (node[1]['X'], -node[1]['Y']) for node in g.nodes(data=True)}
     82 
     83 # Preview of node_positions .

<ipython-input-23-38d6675d5393> in <dictcomp>(.0)
     79 
     80 # Define node positions data structure (dict) for plotting
---> 81 node_positions = {node[0]: (node[1]['X'], -node[1]['Y']) for node in g.nodes(data=True)}
     82 
     83 # Preview of node_positions .

KeyError: 'X'

#this is what I got:
ID    rep1

X 1 Y 1811 Name: 0, dtype: object ID rep2 X 2 Y 1811 Name: 1, dtype: object ID rep4 X 4 Y 1135 Name: 2, dtype: object ID rep5 X 5 Y 420 Name: 3, dtype: object ID rep7 X 7 Y 885 Name: 4, dtype: object ID rep8 X 8 Y 1010 Name: 5, dtype: object ID rep10 X 10 Y 1010 Name: 6, dtype: object ID rep12 X 12 Y 1135 Name: 7, dtype: object ID rep13 X 13 Y 1135 Name: 8, dtype: object ID rep16 X 16 Y 885 Name: 9, dtype: object ID rep17 X 17 Y 1135 Name: 10, dtype: object ID rep19 X 19 Y 1000 Name: 11, dtype: object ID rep26 X 26 Y 850 Name: 12, dtype: object ID rep27 X 27 Y 885 Name: 13, dtype: object no of edges: 38 no of nodes: 16 ID XY 0 rep1 1 1811 1 rep2 2 1811 2 rep4 4 1135 3 rep5 5 420 4 rep7 7 885 5 rep8 8 1010 6 rep10 10 1010 7 rep12 12 1135 8 rep13 13 1135 9 rep16 16 885 10 rep17 17 1135 11 rep19 19 1000 12 rep26 26 850 13 rep27 27 885 X 1 Y 1811名称:0,dtype:对象ID rep2 X 2 Y 1811名称:1,dtype:对象ID rep4 X 4 Y 1135名称:2,dtype:对象ID rep5 X 5 Y 420名称:3,dtype:对象ID rep7 X 7 Y 885名称:4,dtype:对象ID rep8 X 8 Y 1010名称:5,dtype:对象ID rep10 X 10 Y 1010名称:6,dtype:对象ID rep12 X 12 Y 1135名称:7,dtype :对象ID rep13 X 13 Y 1135名称:8,dtype:对象ID rep16 X 16 Y 885名称:9,dtype:对象ID rep17 X 17 Y 1135名称:10,dtype:对象ID rep19 X 19 Y 1000名称:11 ,dtype:对象ID rep26 X 26 Y 850名称:12,dtype:对象ID rep27 X 27 Y 885名称:13,dtype:对象边缘数:38节点数:16 ID XY 0 rep1 1 1811 1 rep2 2 1811 2 rep4 4 1135 3 rep5 5 420 4 rep7 7 885 5 rep8 8 1010 6 rep10 10 1010 7 rep12 12 1135 8 rep13 13 1135 9 rep16 16 885 10 rep17 17 1135 11 rep19 19 1000 12 rep26 26850 13 rep27 27885


Does your example file look exactly like you entered it? 您的示例文件看起来与您输入的文件完全一样吗? For a .csv file I (and the Python csv library) would expect it to be structured more like this: 对于一个.csv文件,我(和Python csv库)希望它的结构更像这样:

ID, X, Y
'rep1', 1, 1811
'rep2', 2, 420
etc....

This would explain why your key's were not as expected. 这可以解释为什么您的密钥不符合预期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM