gpt4 book ai didi

python - 如何在新的 tkinter 窗口上显示词典

转载 作者:太空宇宙 更新时间:2023-11-03 20:43:09 27 4
gpt4 key购买 nike

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

这是我转换为字典的 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

最佳答案

这里不需要用字典转换为列表,使用:

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)

关于python - 如何在新的 tkinter 窗口上显示词典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56749460/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com