我目前要做的项目涉及到从CSV文件中阅读数据,并使用Graphviz来显示数据的可视化表示。这是代码的样子:
import graphviz
import pandas
import os
import math
def save_graph_as_jpg(graph, filename):
graph.save('temp.dot')
src = graphviz.Source.from_file('temp.dot')
src.render(filename, format="jpg")
os.remove(filename)
os.remove('temp.dot')
class Node:
def __init__(self, data, left = None, right = None):
self.left = left
self.right = right
self.data = data
df = pandas.read_csv('decisiontree.csv', index_col = "ID") # df is "data frame"
print(df.to_string())
print(df.info)
nodes = []
nodeMap = {None:None}
for index, row in df[::-1].iterrows():
row = df.index(int[index])
if isinstance(df.loc[row][3], float) and math.isnan(df.loc[row][3]):
df.loc[row][3] = None
if isinstance(df.loc[row][2], float) and math.isnan(df.loc[row][2]):
df.loc[row][2] = None
nodeMap[df.loc[row][0]] = Node(df.loc[row][1],nodeMap[df.loc[row][3]], nodeMap[df.loc[row][2]]), nodes.insert(0,df.loc[row][0])
graph = graphviz.Digraph('structs', filename='structs.gv', node_attr={'shape': 'plaintext', 'ordering':'out'})
for nodeID in nodes:
node = nodeMap[nodeID]
if node.left:
graph.edge(node.data, node.left.data)
if node.right:
graph.edge(node.data, node.right.data)
save_graph_as_jpg(graph, "Decisiontree")
当我用IDLE运行它时,它返回的大部分代码都很好,但是在第27行挂起了:
row = df.index(int[index])
我收到一条追溯消息,内容如下:
Traceback (most recent call last):
File "C:\Users...... line 27, in <module>
row = df.index[index]
File "C:\Users......Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pandas\core\indexes\base.py", line 5382, in __getitem__
result = getitem(key)
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
我把它改成了:
row = df.index(int[index])
现在我得到了一个回溯和索引错误:
Traceback (most recent call last):
File "C:\Users.......CTML AI\Week 3\Lab3.py", line 27, in <module>
row = df.index(int[index])
TypeError: 'type' object is not subscriptable
1条答案
按热度按时间33qvvth11#
您收到该错误是因为您尝试将方括号用于
int
类型,这会尝试将int
当作数组来添加下标。这不起作用,因为int类型为[1]。您可能希望使用圆括号来将index
变量的类型转换为integer。请尝试将第27行更改为1.然而,如果你有一个名为
int
的数组,它就可以工作,但是将你的变量命名为与内置类型或函数相同可能不是一个好主意。