如何将groupby分组表从dataFrame写入word文档? [英] How to write groupby grouped table from dataFrame to word document?

查看:100
本文介绍了如何将groupby分组表从dataFrame写入word文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Pandas 和 python-docx 的新手,在 Pandas 中使用 groupby 有一个这样的表:

I am new to pandas and python-docx, have a table like this using groupby in pandas:

使用 print(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 文档:

I am using the following to write this dataFrame df to 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])

如答案中所述:将 Python Pandas DataFrame 写入 Word 文档

as specified in the answer: 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

我是新手,因此无法弄清楚我哪里出错了,我想打印整个表格?

I am newbie and hence unable to figure out where I am getting wrong, I want to print the whole of table?

推荐答案

看起来您没有将数据帧的 index 值写入文件.我已经修改了您的脚本以显示您可以使用 df.index 来访问这些,然后将它们写入第一列.

It looks like you are not writing the index values of the dataframe to file. I have amended your script to show you can use df.index to access these and then write them to the first column.

#...

t = doc.add_table(df.shape[0]+2, df.shape[1]+1) #df.shape[1]+1 to add one extra column for row names

for j in range(df.shape[-1]):
    t.cell(0,j+1).text = df.columns[j] 

for i in range(df.shape[0]):
    for j in range(df.shape[-1]):
        t.cell(i+2,j+1).text = str(df.values[i,j])

# write columns name to file
t.cell(0,0).text = df.columns.name

# write the index values to file
t.cell(1,0).text = df.index.name

for i, my_index in enumerate(df.index):
     t.cell(i+2,0).text = my_index

这篇关于如何将groupby分组表从dataFrame写入word文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆