pandas 如何从Tkinter Treeview打印数据框?

jjhzyzn0  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(150)

我正在尝试从Tkinter Treeview打印一个数据框。在这种情况下,我总共有10个项目。(查看下面的屏幕截图)x1c 0d1x
通过使用以下代码;

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

正如你所看到的代码,我正在使用打印多个 Dataframe ,这是我在树视图行数相等。请,可以帮助我写的代码,只打印最后一个 Dataframe 与10索引。
我想在候机厅看到这个

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
nnvyjq4y

nnvyjq4y1#

这是因为您正在for循环中调用print方法。
从循环中删除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

相关问题