Pandastable将新列添加到现有数据框

时间:2020-08-23 22:23:15

标签: python pandas tkinter

我正在尝试在脚本中使用pandastable库。基本上,以下脚本将用户选择的.csv导入到pandastable中,并正确显示在tkinter GUI上。

一旦导入,我想添加列。我认为使用此文档 https://pandastable.readthedocs.io/en/latest/examples.html#basics 及其列出的表格方法将很容易。例如:table.autoAddColumns(1)仅添加一列,但是无论我如何尝试和使用它,我都无法使其正常工作。

import csv
import tkinter as tk
import tkinter.ttk as tkrttk
from tkinter import *
from tkinter import filedialog

import pandas as pd
from pandastable import Table, TableModel
from PIL import Image, ImageFont, ImageTk

root = tk.Tk()
root.geometry("2000x1000")
root.title('Workshop Manager')
style = tkrttk.Style()
style.configure("Treeview.Heading", foreground='Red', font=('Helvetica', 10))


def select_input_file():
    global input_file_path
    input_file_path = filedialog.askopenfilename(
    filetypes=(("CSV files", "*.csv"),))
    app = TestApp(root, input_file_path)
    app.place(bordermode = INSIDE,height = 500, width = 2000, x =0, y=50)

class TestApp(tk.Frame):
     def __init__(self, parent, input_file_path):
        super().__init__(parent)
        self.table = Table(self, showtoolbar=False, showstatusbar=False)
        self.table.importCSV(input_file_path)
        self.table.show(input_file_path)
        ##Breaks here##
        self.table.autoAddColumns(1)

root.mainloop()

我尝试使用table.autoAddColumns(1)。这也不起作用。

如果我使用self.table.autoAddColumns(1),则会收到错误消息AttributeError: 'TableModel' object has no attribute 'auto_AddColumns'

如果我使用table.autoAddColumns(1),则会收到错误消息NameError: name 'table' is not defined

1 个答案:

答案 0 :(得分:2)

这是pandastable中的错误。

但是,您可以改为使用addColumn()函数。在不带参数的情况下调用它时,将显示一个对话框来选择dtype并输入新列的名称。