I've been importing CSVs using pandas, but I keep getting a random extra line every time I try to use it and it causes errors in my code. How do I completely erase this line?
The code I used to import it was: import itertools import copy import networkx as nx import pandas as pd import matplotlib.pyplot as plt import csv
df3=pd.read_csv(r"U:\\user\edge_list_4.csv")
print(df3)
df4=pd.read_csv(r"U:\\user\nodes_fixed_2.csv")
df4.dropna()
print(df4)
g=nx.Graph()
for i,elrow in df3.iterrows():
g.add_edge(elrow[0], elrow[1], **elrow[2:].to_dict())
# Add node attributes
for i, nlrow in df4.iterrows():
# g.node[nlrow['id']] = nlrow[1:].to_dict() # deprecated after NX 1.11
nx.set_node_attributes(g, {nlrow['ID']: nlrow[1:].to_dict()})
# Node list example
print(nlrow)
# Preview first 5 edges
list(g.edges(data=True))[0:5]
# Preview first 10 nodes
list(g.nodes(data=True))[0:10]
print('# of edges: {}'.format(g.number_of_edges()))
print('# of nodes: {}'.format(g.number_of_nodes()))
# Define node positions data structure (dict) for plotting
for node in g.nodes(data=True):
print(node)
print("")
node_positions = {node[0]: (node[1]['X'], -node[1]['Y']) for node in
g.nodes(data=True)}
My table is a simple ID, X ,Y table. I've tried using the:
drop.na()
code, but couldn't seem to take it away. I've tried editing it on Notepad++ and import it as a txt file, but it still keeps appearing. Is there any way I should specifically edit the csv file on excel or is there a code I can use?
('rep1', {'X': 1, 'Y': 1811})
('rep2', {'X': 2, 'Y': 1811})
('rep3', {'X': 3, 'Y': 1135})
('rep4', {'X': 4, 'Y': 420})
('rep5', {'X': 5, 'Y': 885})
('rep6', {'X': 6, 'Y': 1010})
('rep7', {'X': 7, 'Y': 1010})
('rep8', {'X': 8, 'Y': 1135})
('rep9', {'X': 9, 'Y': 1135})
('rep10', {'X': 10, 'Y': 885})
('rep1 ', {})
The line is only meant to the rep 10.
KeyError: 'X'