#把Excel中的数据填写到固定的矩阵表头中,对应矩阵位置没有的写0,有的写Excel中的数据。
不知道你想填到怎么样的矩阵中?读入的excel数据就是一个二维数组
先定义一个元素为0的待填充矩阵。遍历元素在相应位置将读取的表格数据填入矩阵中,其他位置仍为0.参考代码:
df=pd.read_excel('a.xlsx')
print(df.values)
arr=np.zeros((6,6))
rs,cs=df.shape
for r in range(rs):
for c in range(cs):
arr[r][c]=df.values[r][c]
print(arr)
运行结果:
[[0 0 1]
[1 0 1]
[0 2 2]
[2 0 2]
[0 1 0]]
[[0. 0. 1. 0. 0. 0.]
[1. 0. 1. 0. 0. 0.]
[0. 2. 2. 0. 0. 0.]
[2. 0. 2. 0. 0. 0.]
[0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]
如对你有帮助,请点击一下采纳按钮。