Im trying to obtain with PYSimpleGUI table with formatted numbers (no decimals numbers). I take data from Dataframe and able to create table but without formatting. If I try to format numbers on dataframe directly with style.format
then during Table creation I recieve "Styler error". So I understand that I have first push DF into Table creation and then somehow format values. How to change format to {:,.0f}
in table view. I looked for examples and cookbook but found no information about that.
import pysimplegui as sg
import pandas as pd
df = pd.DataFrame([[100.045, 212504.4588], [34658.13489, 445598.465498], [546589.466, 646549.4847], [71214.4986, 8498779.46598]], columns=["A", "B"])
layout = [[sg.Table(values=df,
display_row_numbers=True,
auto_size_columns=False,
num_rows=min(25, len(data)))]
window = sg.Window('Table', layout, grab_anywhere=False)
event, values = window.read()
window.close()
So far I receive table that looks like:
A B
0 100.045 212504.4588
1 34658.13489 445598.465498
....
I would like to achieve table that looks like:
A B
0 100 212,504
1 34,658 445,598
....
Along with a few other fixes to your code, you need to use the package numpy
as well to change the dtype
from float64 to int64 shown in the code below
import PySimpleGUI as sg
import pandas as pd
import numpy as np
def df_gui():
df = pd.DataFrame(columns=["A", "B"],
data=np.array([[100.045, 212504.4588], [34658.13489, 445598.465498], [546589.466, 646549.4847],
[71214.4986, 8498779.46598]]))
df['A'] = df['A'].astype(np.int64)
df['B'] = df['B'].astype((np.int64))
values = df.values.tolist()
headers = df.columns.values.tolist()
layout = [[sg.Table(headings=headers, values=values, display_row_numbers=True, auto_size_columns=False, num_rows=min(25, len(values)))]]
window = sg.Window('Table', layout, grab_anywhere=False)
while True:
event, value = window.read()
if event == sg.WIN_CLOSED:
break
window.close()
df_gui()
TIP: run event, values = window.read()
in a while True:
loop. this will keep the window open, reading the event and values when ever something is clicked, until the x is pressed in the window which breaks
the loop
You need to convert the data to requested format by yourself.
import PySimpleGUI as sg
import pandas as pd
data = [
[100.045, 212504.4588],
[34658.13489, 445598.465498],
[546589.466, 646549.4847],
[71214.4986, 8498779.46598],
]
df = pd.DataFrame(data, columns=["A", "B"])
table_data = list(map(lambda x:list(map(lambda d:f'{d:,.0f}', x)), df.values.tolist()))
table_headers = df.columns.values.tolist()
layout = [[sg.Table(table_data, headings=table_headers, display_row_numbers=True,
auto_size_columns=False, num_rows=min(25, len(data)))]]
sg.Window('Table', layout, grab_anywhere=False).read(close=True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.