我对pandas和python docx不熟悉,在pandas中使用groupby有这样一个表:
用print(df)
打印df
输出:
Change Type typeA typeB typeC typeD typeE typeF
Component
A 0 2 6 0 0 6
B 0 3 2 1 1 3
C 0 1 0 0 0 4
D 0 2 2 0 0 3
E 0 0 0 0 0 1
F 0 3 0 0 1 2
G 2 1 3 0 2 3
H 0 0 0 0 0 1
I 0 1 0 0 0 0
我使用以下方法将此数据帧df
写入word document
:
t = doc.add_table(df.shape[0]+1, df.shape[1])
for j in range(df.shape[-1]):
t.cell(0,j).text = df.columns[j]
for i in range(df.shape[0]):
for j in range(df.shape[-1]):
t.cell(i+1,j).text = str(df.values[i,j])
如答案所述:
Writing a Python Pandas DataFrame to Word document
我正在打印以下内容:
typeA typeB typeC typeD typeE typeF
0 2 6 0 0 6
0 3 2 1 1 3
0 1 0 0 0 4
0 2 2 0 0 3
0 0 0 0 0 1
0 3 0 0 1 2
2 1 3 0 2 3
0 0 0 0 0 1
0 1 0 0 0 0
我是新手,因此无法找出哪里我错了,我想打印整个表?你知道吗
看起来您没有将数据帧的
index
值写入文件。我已经修改了您的脚本,以显示您可以使用df.index
访问这些内容,然后将它们写入第一列。你知道吗相关问题 更多 >
编程相关推荐