将值保存在数据框中,并将其替换为csv



所以我不太确定如何表述这个问题,因为我在python和编程方面都是新手。我有一个GUI,显示已经可用的信息从csv:

def updatetext(self):
"""adds information extracted from database already provided"""

df_subj = Content.extract_saved_data(self.date)
self.lineEditFirstDiagnosed.setText(str(df_subj["First_Diagnosed_preop"][0])) 
if str(df_subj["First_Diagnosed_preop"][0]) != 'nan' else self.lineEditFirstDiagnosed.setText('')
self.lineEditAdmNeurIndCheck.setText(str(df_subj['Admission_preop'][0])) 

是伟大的

现在,如果我更改GUI中的值,我希望它们在csv中更新。我是这样开始的:

def onClickedSaveReturn(self):
"""closes GUI and returns to calling (main) GUI"""
df_general = Clean.get_GeneralData()
df_subj = {k: '' for k in Content.extract_saved_data(self.date).keys()} # extract empty dictionary
df_subj['ID'] = General.read_current_subj().id[0]
df_subj['PID'] = df_general['PID_ORBIS'][0]
df_subj['Gender'] = df_general['Gender'][0]
df_subj['Diagnosis_preop'] = df_general['diagnosis'][0]
df_subj["First_Diagnosed_preop"] = self.lineEditFirstDiagnosed.text()
df_subj['Admission_preop'] = self.lineEditAdmNeurIndCheck.text()
df_subj['Dismissal_preop'] = self.DismNeurIndCheckLabel.text()

,这是我的老板现在添加的:

subj_id = General.read_current_subj().id[0] # reads data from curent_subj (saved in ./tmp)
df = General.import_dataframe('{}.csv'.format(self.date), separator_csv=',')
if df.shape[1] == 1:
df = General.import_dataframe('{}.csv'.format(self.date), separator_csv=';')
idx2replace = df.index[df['ID'] == subj_id][0]
# TODO: you need to find a way to turn the dictionaryy df_subj into a dataframe and replace the data at
#  the index idxreplace of 'df' with df_subj. Later I would suggest to use line 322 to save everything to the
#  file
df.iloc[idx2replace] = pds.DataFrame([df_subj])
df.to_csv("preoperative.csv", index=False)
# df.to_csv(os.path.join(FILEDIR, "preoperative.csv"), index=False)
self.close()

我真的不知道如何处理这个问题,或者老实说,我不知道该怎么做。

希望有人能帮助我。

谢谢你

您应该只加载文件一次,并保留DF (self.df或其他)。然后显示它,每次用户在GUI中更改值时,DF应该更新,当用户单击保存时,您应该用内存中的当前DF覆盖现有文件。

相关内容

  • 没有找到相关文章

最新更新