我正在运行此代码,并且在某些时候遇到代码问题。
a = ['C', 'c1', 'c2']
b = ['p3', 'p1', 'p4', 'p2']
print(a)
print (b)
df = pd.concat([pd.DataFrame(x, columns=b, index=a) for x in matrix]),keys=a)
print(df)
这是我所遇到的错误的输出:
['C', 'c1', 'c2']
['p3', 'p1', 'p4', 'p2']
File "M:\Eclipse\hey\Main_Graph.py", line 83, in Main_Graph
df = pd.concat([pd.DataFrame(x, columns=b, index=a) for x in matrix])
File "M:\Eclipse\hey\Main_Graph.py", line 83, in <listcomp>
df = pd.concat([pd.DataFrame(x, columns=b, index=a) for x in matrix])
NameError: name 'b' is not defined
我想要这种输出:
C c1 c2
C p1 0.0 0.0 0.0
p2 0.0 0.0 0.0
p3 0.0 0.0 0.0
p4 0.0 0.0 0.0
c1 p1 0.0 0.0 0.0
p2 0.0 0.0 0.0
p3 0.0 0.0 0.0
p4 0.0 0.0 0.0
c2 p1 0.0 0.0 0.0
p2 0.0 0.0 0.0
p3 0.0 0.0 0.0
p4 0.0 0.0 0.0