如何在新的Tkinter窗口上显示字典

时间:2019-06-25 08:03:43

标签: python python-3.x pandas tkinter

我正在处理excel文件,以在新的tkinter窗口上显示数据。

这是我转换为dict的Excel数据:

{'Country': {0: 'Japan', 1: 'China', 2: 'USA', 3: 'Russia', 4: 'Japan', 
5: 'Japan', 6: 'China'}, 'Port': {0: 'Yokohama', 1: 'Ningbo', 2: 
'Baltimore', 3: 'Moscow', 4: 'Tokyo', 5: 'Tokyo', 6: 'Shanghai'}, 
'incoterm': {0: 'FOB', 1: 'DAT', 2: 'FOB', 3: 'EXW', 4: 'FOB', 5: 'FOB', 
6: 'EXW'}, 'Capacity': {0: '40ton', 1: '40ton', 2: 'Other', 3: '20ton', 
4: '20ton', 5: 'Other', 6: '40ton'}, 'Date': {0: nan, 1: nan, 2: nan, 3: 
nan, 4: nan, 5: nan, 6: nan}, 'Oct': {0: 400, 1: 500, 2: 600, 3: 100, 4: 
400, 5: 500, 6: 120}, 'Nov': {0: 500, 1: 200, 2: 200, 3: 300, 4: 500, 5: 
600, 6: 985}, 'Dec': {0: 100, 1: 200, 2: 800, 3: 400, 4: 200, 5: 100, 6: 
146}, '$ value': {0: 2650.6, 1: 2650.6, 2: 2650.6, 3: 2650.6, 4: 2650.6, 
5: 2650.6, 6: 2500.6}, 'Total': {0: 2650600.0, 1: 2385540.0, 2: 
4240960.0, 3: 2120480.0, 4: 2915660.0, 5: 3180720.0, 6: 3128250.6}}

我到目前为止所得到的:

import pandas as pd
from tkinter import *
from tkinter import ttk
df = pd.read_excel("some excel data")

df = df.to_dict()
a = []
a.append(dict(df))
print(a)

root = Tk()

for data in a:
    temp_text = '{0} {1} - ({2})'.format(data['Country'], 
    data['incoterm'], data['Total'])
    ttk.Label(root, text=temp_text).pack()

mainloop()

输出:

{0: 'Japan', 1: 'China', 2: 'USA', 3: 'Russia', 4: 'Japan', 5: 'Japan', 
6: 'China'}{0: 'FOB', 1: 'DAT', 2: 'FOB', 3: 'EXW', 4: 'FOB', 5: 'FOB', 
6: 'EXW'}-({0: 2650600.0, 1: 2385540.0, 2: 4240960.0, 3: 2120480.0, 4: 
2915660.0, 5: 3180720.0, 6: 3128250.6}})

预期输出:

Japan FOB -(2650600.0)
China EXW -(2385540.0)
....etc

1 个答案:

答案 0 :(得分:0)

此处无需转换为带有字典的列表,请使用:

df = pd.DataFrame(a)
print (df)
  Country       Port incoterm Capacity  Date  Oct  Nov  Dec  $ value  \
0   Japan   Yokohama      FOB    40ton   NaN  400  500  100   2650.6   
1   China     Ningbo      DAT    40ton   NaN  500  200  200   2650.6   
2     USA  Baltimore      FOB    Other   NaN  600  200  800   2650.6   
3  Russia     Moscow      EXW    20ton   NaN  100  300  400   2650.6   
4   Japan      Tokyo      FOB    20ton   NaN  400  500  200   2650.6   
5   Japan      Tokyo      FOB    Other   NaN  500  600  100   2650.6   
6   China   Shanghai      EXW    40ton   NaN  120  985  146   2500.6   

       Total  
0  2650600.0  
1  2385540.0  
2  4240960.0  
3  2120480.0  
4  2915660.0  
5  3180720.0  
6  3128250.6 

您可以通过将数字列转换为字符串来连接所有列:

s = df['Country'] + ' ' + df['incoterm'] + ' - (' +  df['Total'].astype(str) + ')'
for temp_text in s: 
    print (temp_text)

或使用DataFrame.itertuples

for data in df.itertuples():
    temp_text = '{0} {1} - ({2})'.format(data.Country, data.incoterm, data.Total)
    print (temp_text)

如果性能并不重要,请使用DataFrame.iterrows,但效果最慢:

for i, data in df.iterrows():
    temp_text = data
    temp_text = '{0} {1} - ({2})'.format(data['Country'], data['incoterm'], data['Total'])
    print (temp_text)
Japan FOB - (2650600.0)
China DAT - (2385540.0)
USA FOB - (4240960.0)
Russia EXW - (2120480.0)
Japan FOB - (2915660.0)
Japan FOB - (3180720.0)
China EXW - (3128250.6)