如何从pandastable保存数据?

时间:2019-01-18 17:14:20

标签: python python-3.x pandas dataframe

我正在创建一个表接口,用户将在其中从pandastable接收一个表并写入一些数据。然后,我需要保存更新的pandastable进行评估。我该怎么办?

这是代码:

from tkinter import *
from pandastable import Table, TableModel
import pandas as pd

class TestApp(Frame):
  def __init__(self, parent=None):
    self.parent = parent
    Frame.__init__(self)
    self.main = self.master
    self.main.geometry('600x400+200+100')
    self.main.title('Table app')
    f = Frame(self.main)
    f.pack(fill=BOTH,expand=1)
    df = pd.read_excel('2018.xlsx','November',7)
    x = 'desc'
    nome = df[x].values.tolist()
    name = []
    for i in range(len(nome)):
        if nome[i] == nome[i] and nome[i] != x:
            name.append(nome[i])

    df1 = pd.DataFrame({x:name})
    cfop = [float('Nan') for i in range(len(name))]
    cst_in = [float('Nan') for i in range(len(name))]
    cst_out = [float('Nan') for i in range(len(name))]
    ncm = [float('Nan') for i in range(len(name))]
    icms = [float('Nan') for i in range(len(name))]
    df1['ncm'] = pd.Series(ncm)
    df1['CST in'] = pd.Series(cst_in)
    df1['CST out'] = pd.Series(cst_out)
    df1['CFOP'] = pd.Series(cfop)
    df1['ICMS'] = pd.Series(icms)
    self.table = pt = Table(f, dataframe=df1, showtoolbar=True, showstatusbar=True)
    pt.show()
    return 
app = TestApp()
app.mainloop()

df1 = Table.model.df1

用户应填写ncmCFOPICMSCST inCST out列。现在,他可以在这些列上书写,但是一旦关闭应用程序,数据就会丢失。我想获取他编写的数据并将其放入变量DataFrame中。

1 个答案:

答案 0 :(得分:1)

您可以使用以下行:

pt.doExport(filename="test2.csv")

这将产生一个.csv文件,其中包含表中的所有数据。