遍历电子表格并使用xlsxwriter,Pandas绘制所有列

时间:2019-03-16 21:24:38

标签: python pandas loops xlsxwriter

我敢打赌,我真的很靠近这里。我正在尝试查看可能包含100列的电子表格,并在新的电子表格中为每列动态创建一个绘图。在其中的一些工作中,我只需多次调用chart1,chart2,chart3 ......

我在下面的内容可以很好地循环,将数据插入第一张表中,创建第二张表并仅插入第一张图表。如何编写循环以创建“ n”张图表?

我敢打赌,这是一件愚蠢的琐事。

谢谢。

输入数据: https://drive.google.com/open?id=1sts5axnT7aQ04zHv8nPwhnrQPDb7oZlV

import pandas as pd
import codecs
import csv
import os
import xlsxwriter
import datetime
df3 = pd.read_csv('TEST.csv', index_col=0, header=[0], low_memory=False, na_filter=True, encoding='utf-8')
writer2 = pd.ExcelWriter('TEST.xlsx', engine='xlsxwriter')
#define the sheetname
stage = 10
sheetname1 = "Stage_" +str(stage)
print(sheetname1)
df3.to_excel(writer2, sheet_name = sheetname1 , startrow=4, startcol=0, encoding='utf8')
maxcol = df3.shape[1]-1
maxlen = df3.shape[0]-1
print(maxcol, maxlen)
#set the workbook value
c = df3[['TWO']]
workbook = writer2.book
#set the worksheet value
sheetname2 = "Stage_" +str(stage) +str("_P")
c.to_excel(writer2, sheet_name = sheetname2 , startrow=4, startcol=0, encoding='utf8')
print(sheetname2)
worksheet = writer2.sheets[ sheetname2 ]
for i in range(2, maxcol):
    TITLE = df3.columns[i]
    LOC_NUM = (i - 1) * 34 - 33
    LOCATION = "C" +str(LOC_NUM)
    print("TITLE:", TITLE, "GRAPH_LOCATION:", LOC_NUM,LOCATION, "LENGTH:", maxlen, "INDICE:", i)
    chart = workbook.add_chart({'type': 'line'})
    chart.set_size({'width': 1200, 'height': 640})
    chart.add_series({"values" : [ sheetname1 , 7, i ,maxlen, i ],"name" : TITLE })
    chart.set_x_axis({'name': 'Time (s)', 'position_axis': 'on_tick'})
    chart.set_y_axis({'name': 'Test', 'major_gridlines': {'visible': False}})
    # Turn off chart legend. It is on by default in Excel.
    chart.set_legend({'position': 'none'})
    worksheet.insert_chart( LOCATION , chart )
    writer2.save()

0 个答案:

没有答案