使用 python 创建绘图,同时将 excel 数据分离到新文件

Create plots using python while separating excel data to new files

首先,我是 Python 和一般编码的新手。我可以根据特定列中的唯一值成功分离我的数据,但随着 for 循环的迭代,我还想创建一个图。出于某种原因,python 正在创建单独的 Excel 工作表,但完全忽略了情节。任何 advice/pointing 我做错的地方都将不胜感激。

import pandas as pd
import xlsxwriter

excel_file = 'CR1000XSeries_LiftoffData.xlsx'

df = pd.read_excel(excel_file, sheet_name='CR1000XSeries_LiftoffData')

split_values = df['StrandID'].unique()

for value in split_values:
    df1 = df[df['StrandID'] == value]
    output_file_name = "StrandID" + str(value) + "Test.xlsx"

    workbook = xlsxwriter.Workbook(output_file_name)
    ws = workbook.add_worksheet('Sheet1')


    rows = len(df1)

    r1 = 1
    r2 = rows
    C1 = 0
    C2 = 9
    C3 = 14

    chart = workbook.add_chart({'type': 'scatter', 'subtype': 'straight'})

    chart.add_series({
        'categories': ['Sheet1', r1, C1, r2, C1],
        'values': ['Sheet1', r1, C2, r2, C2],
        'name': 'JackPressurePSI'})

    # Second Y axis
    chart.add_series({
        'categories': ['Sheet1', r1, C1, r2, C1],
        'values': ['Sheet1', r1, C3, r2, C3],
        'name': 'AbsoluteDispIN',
        'y2_axis': 1})

    ws.insert_chart('B10', chart)

    df1.to_excel(output_file_name, index=False)

主要问题是程序正在用 df1.to_excel() 创建的同名文件覆盖 xlsxwriter.Workbook() 创建的 xlsx 文件。

请参阅 Working with Python Pandas and XlsxWriter 上的 XlsxWriter 文档,了解一起使用 Pandas 和 XlsxWriter 的正确方法。

修复该程序应该可以正常工作的问题。这是一个更新版本:

import pandas as pd

excel_file = 'CR1000XSeries_LiftoffData.xlsx'

df = pd.read_excel(excel_file, sheet_name='CR1000XSeries_LiftoffData')

split_values = df['StrandID'].unique()

for value in split_values:
    output_df = df[df['StrandID'] == value]
    output_file_name = "StrandID" + str(value) + "Test.xlsx"

    # Create a pandas excel writer using XlsxWriter as the engine.
    writer = pd.ExcelWriter(output_file_name, engine='xlsxwriter')
    output_df.to_excel(writer, sheet_name='Sheet1')

    # Get the xlsxwriter workbook and worksheet objects.
    workbook = writer.book
    worksheet = writer.sheets['Sheet1']

    max_row = df.shape[0]

    row1 = 1
    row2 = max_row
    col1 = 0
    col2 = 9
    col3 = 14

    chart = workbook.add_chart({'type': 'scatter', 'subtype': 'straight'})

    chart.add_series({
        'categories': ['Sheet1', row1, col1, row2, col1],
        'values': ['Sheet1', row1, col2, row2, col2],
        'name': 'JackPressurePSI'})

    # Second Y axis
    chart.add_series({
        'categories': ['Sheet1', row1, col1, row2, col1],
        'values': ['Sheet1', row1, col3, row2, col3],
        'name': 'AbsoluteDispIN',
        'y2_axis': True})

    worksheet.insert_chart('B10', chart)

    writer.save()

我无法对此进行全面测试,因为我没有输入文件,但它可以正确编译。另外,我更改了一些变量名,使它们更明确。