对生成的csv表使用内部函数后,列名不更新

zz2j4svz  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(182)

我在处理PySimpleGUI程序中的一段代码时遇到了问题。为了尽可能直接,这里是正在执行的步骤。

  • 上载CSV文件并创建可编辑的csv窗口
  • 按下按钮(“Calculate Diplotype”),并调用内部函数(diplotype_calc())
  • 然后,csv窗口将使用操作后的Dataframe更新

问题出在最后一步。数据按照内部函数的预期正确操作,但对于我来说,我无法让csv窗口显示新的列名,它们与初始上传相同。有什么想法我可以解决这个更新问题?
我将只添加内部函数,因为我认为这就是问题所在。外部函数csvwindow()创建csv窗口并显示上载的csv。内部函数diplotype_calc()执行所有操作。我还将在顶部添加我正在使用的导入。

import PySimpleGUI as sg
import webview
import pandas as pd
import csv
import os
import operator
import tkinter as tk
from tkinter import filedialog
import tkinter.messagebox

# Diplotype Calculator Functions
    def diplotype_calc(file_path):
        data, allele_cols = read_csv_file(file_path)
        allele_df = pd.DataFrame(data, columns=allele_cols)[['sample ID', 'CYP2D6', 'CYP2C9']]
        print(allele_df)
        # Import CPIC dataframe from Google Cloud Storage into pandas dataframe
        # Currently only supported for CYP2D6 and CYP2C9
        cpic_data = f'https://storage.googleapis.com/pg-genotyping-cpic-2d6-2d9/2d6_2d9_joined.csv'
        cpic_df = pd.read_csv(cpic_data)

        # Merge the two DataFrames based on the common columns CYP2D6 and CYP2C9
        merged_df = pd.merge(allele_df, cpic_df, on=['CYP2D6', 'CYP2C9'])

        # Create two new columns to store the Metabolizer Status and Activity Score
        merged_df['Metabolizer Status'] = ''
        merged_df['Activity Score'] = ''

        # Create two dictionaries to store the metabolizer status and activity score for each call
        d6_status_dict = dict(zip(cpic_df['CYP2D6'], cpic_df['Metabolizer Status']))
        d9_status_dict = dict(zip(cpic_df['CYP2C9'], cpic_df['Metabolizer Status']))
        d6_activity_dict = dict(zip(cpic_df['CYP2D6'], cpic_df['Activity Score']))
        d9_activity_dict = dict(zip(cpic_df['CYP2C9'], cpic_df['Activity Score']))

        # Create two new columns to store the Metabolizer Status and Activity Score
        allele_df['Metabolizer Status'] = ''
        allele_df['Activity Score'] = ''

        # Iterate over each row of the allele DataFrame
        for index, row in allele_df.iterrows():
            # Get the cyp2d6 and cyp2c9 calls for the current sample ID
            cyp2d6_call = row['CYP2D6']
            cyp2c9_call = row['CYP2C9']

            # Use the dictionaries to get the metabolizer status and activity score for the current calls
            cyp2d6_status = d6_status_dict.get(cyp2d6_call, 'UND')
            cyp2c9_status = d9_status_dict.get(cyp2c9_call, 'UND')
            cyp2d6_activity = d6_activity_dict.get(cyp2d6_call, 'UND')
            cyp2c9_activity = d9_activity_dict.get(cyp2c9_call, 'UND')

            # Use the comparison results to populate the Metabolizer Status and Activity Score columns of the allele DataFrame
            allele_df.loc[index, 'Metabolizer Status'] = f'2D6: {cyp2d6_status} ||| 2C9: {cyp2c9_status}'
            allele_df.loc[index, 'Activity Score'] = f'{cyp2d6_activity}, {cyp2c9_activity}'

        # convert all of the updated data back into a list of lists
        updated_data = allele_df.values.tolist()
        # save_loc(allele_df)
        print(updated_data)
        window['-TABLE-'].update(values=updated_data)

        csv_window(updated_data)

如果需要,我可以提供外部功能。提前感谢您的想法!

9fkzdhlc

9fkzdhlc1#

下面的语句只更新表中的数据

window['-TABLE-'].update(values=updated_data)

没有提供更新Table元素标题的方法。
演示代码与tkinter代码.

import PySimpleGUI as sg

font = ("Courier New", 20)

layout = [
    [sg.Table(
        [[f'Cell ({i}, {j})' for j in range(2)] for i in range(5)],
        headings=[f'Heading {i} - 0' for i in range(2)],
        justification='center',
        key='TABLE')],
    [sg.Push(), sg.Button('Update')],
]
window = sg.Window('Title', layout)
count = 0

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == "Update":
        count += 1
        for i in range(2):  # Update new headings
            window['TABLE'].widget.heading(i, text=f'Heading {i} - {count}')

window.close()

相关问题