【问题标题】:How can I print a Dataframe from Tkinter Treeview?如何从 Tkinter Treeview 打印数据框?
【发布时间】:2021-11-02 19:24:30
【问题描述】:

我正在尝试从 Tkinter Treeview 打印数据框。在这种情况下,我总共有 10 个项目。 (查看下面的截图)

通过使用以下代码;

row_list = []
 columns = ('Index','DrugsName', 'NumberWasted','MoneyWasted','Date')
 for child in treeview.get_children():
     row_list.append(treeview.item(child)["values"])
    
      treeview_df = pd.DataFrame(row_list, columns=columns)
    

     print(treeview_df)

我在终端得到这个输出;

Index   DrugsName  NumberWasted  MoneyWasted     Date
0      1  vancomycin             1          300  11/2/21
   Index   DrugsName  NumberWasted  MoneyWasted     Date
0      1  vancomycin             1          300  11/2/21
1      2   meropenem             2          150  11/2/21
   Index   DrugsName  NumberWasted  MoneyWasted     Date
0      1  vancomycin             1          300  11/2/21
1      2   meropenem             2          150  11/2/21
2      3  gentamicin             3          200  11/2/21
   Index   DrugsName  NumberWasted  MoneyWasted     Date
0      1  vancomycin             1          300  11/2/21
1      2   meropenem             2          150  11/2/21
2      3  gentamicin             3          200  11/2/21
3      4  micafungin             1           90  11/2/21
   Index     DrugsName  NumberWasted  MoneyWasted     Date
0      1    vancomycin             1          300  11/2/21
1      2     meropenem             2          150  11/2/21
2      3    gentamicin             3          200  11/2/21
3      4    micafungin             1           90  11/2/21
4      5  amphotericin             3          400  11/2/21
   Index     DrugsName  NumberWasted  MoneyWasted     Date
0      1    vancomycin             1          300  11/2/21
1      2     meropenem             2          150  11/2/21
2      3    gentamicin             3          200  11/2/21
3      4    micafungin             1           90  11/2/21
4      5  amphotericin             3          400  11/2/21
5      6     Cefazolin             2          250  11/2/21
   Index     DrugsName  NumberWasted  MoneyWasted     Date
0      1    vancomycin             1          300  11/2/21
1      2     meropenem             2          150  11/2/21
2      3    gentamicin             3          200  11/2/21
3      4    micafungin             1           90  11/2/21
4      5  amphotericin             3          400  11/2/21
5      6     Cefazolin             2          250  11/2/21
6      7      Oxytocin             2         2000  11/2/21
   Index     DrugsName  NumberWasted  MoneyWasted     Date
0      1    vancomycin             1          300  11/2/21
1      2     meropenem             2          150  11/2/21
2      3    gentamicin             3          200  11/2/21
3      4    micafungin             1           90  11/2/21
4      5  amphotericin             3          400  11/2/21
5      6     Cefazolin             2          250  11/2/21
6      7      Oxytocin             2         2000  11/2/21
7      8       Insulin             1         1500  11/2/21
   Index     DrugsName  NumberWasted  MoneyWasted     Date
0      1    vancomycin             1          300  11/2/21
1      2     meropenem             2          150  11/2/21
2      3    gentamicin             3          200  11/2/21
3      4    micafungin             1           90  11/2/21
4      5  amphotericin             3          400  11/2/21
5      6     Cefazolin             2          250  11/2/21
6      7      Oxytocin             2         2000  11/2/21
7      8       Insulin             1         1500  11/2/21
8      9      Thiamine             1          100  11/2/21
   Index     DrugsName  NumberWasted  MoneyWasted     Date
0      1    vancomycin             1          300  11/2/21
1      2     meropenem             2          150  11/2/21
2      3    gentamicin             3          200  11/2/21
3      4    micafungin             1           90  11/2/21
4      5  amphotericin             3          400  11/2/21
5      6     Cefazolin             2          250  11/2/21
6      7      Oxytocin             2         2000  11/2/21
7      8       Insulin             1         1500  11/2/21
8      9      Thiamine             1          100  11/2/21
9     10  Penicillin G             2          250  11/2/21

如您所见,我正在使用的代码打印多个数据帧,这些数据帧等于我在 Treeview 中的行数。拜托,可以帮助我编写仅打印具有 10 个索引的最后一个 Dataframe 的代码。

我想在终端中看到这个;

  
   Index     DrugsName  NumberWasted  MoneyWasted     Date
0      1    vancomycin             1          300  11/2/21
1      2     meropenem             2          150  11/2/21
2      3    gentamicin             3          200  11/2/21
3      4    micafungin             1           90  11/2/21
4      5  amphotericin             3          400  11/2/21
5      6     Cefazolin             2          250  11/2/21
6      7      Oxytocin             2         2000  11/2/21
7      8       Insulin             1         1500  11/2/21
8      9      Thiamine             1          100  11/2/21
9     10  Penicillin G             2          250  11/2/21

【问题讨论】:

    标签: python pandas dataframe tkinter


    【解决方案1】:

    这是因为您在 for 循环中调用 print 方法。

    从循环中删除打印语句应该可以解决问题。

     row_list = []
     columns = ('Index','DrugsName', 'NumberWasted','MoneyWasted','Date')
     
     for child in treeview.get_children():
         row_list.append(treeview.item(child)["values"])
    
     """
     The line below should also be removed from the for loop 
     because you are re initializing the data frame 
     on every iteration of the loop. """
     
     treeview_df = pd.DataFrame(row_list, columns=columns)     
    
     print(treeview_df) # Notice here the difference in indention level of this print statement
    

    【讨论】:

    • 非常感谢您的帮助!
    • 为了提高速度和效率,您还可以将treeview_df = pd.DataFrame(row_list, columns=columns) 行缩进到print(treeview_df) 的级别。它实际上会做同样的事情,但耗时更少,因为当只使用最后一个值时,它不会重复分配treeview_df。 @TheoN:如果这个答案令人满意,请采纳。
    • 好点!并且绝对应该完成。很高兴你抓住了!这是漫长的一天工作!泰西尔维斯特!
    • 我根据西尔维斯特的评论更新了我的答案
    猜你喜欢
    • 2021-07-25
    • 2018-11-17
    • 1970-01-01
    • 2020-12-06
    • 2014-05-02
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多