Synchronize Pandas Dataframe With Pyqt5 Qtableview
I have an editable QTableView which reads the values from a pandas DataFrame. What I'm looking for is that when I change the value of one cell, the pandas DataFrame synchronizes a
Solution 1:
The solution is so simple that I don't understand it. If someone could just explain to me what's going on.
I point out the changes I've made in run.py
:
run.py edited
classMainWindow(QMainWindow):
def__init__(self, parent=None):
QMainWindow.__init__(self, parent)
uic.loadUi('table.ui', self) # --- Loading from Qt Designer. ---# Packages.# ---------
self.pd = pd
# Instancies of class.# --------------------
self.cnt_init_val = CntInitialValues(self)
# Attributes of the instance.# ---------------------------
self.df_table = pd.DataFrame()
self.model = ''# --- Instance variable for model. ---# PyQt5 objects.# --------------
self.tableview = self.tableView
self.btn_print_df = self.pushButtonPrintDataFrame # --- pushButton to print DataFrame. ---# Add initial values to QTableView and show them.# -----------------------------------------------
self.df_table = self.cnt_init_val.dataframe_initial()
self.cnt_init_val.initial_values(self.df_table)
# Events.# -------
self.btn_print_df.clicked.connect(self.dataframe_output) # --- pushButton prints DataFrame. ---# PyQt5 outputs.# ==============deftable_output(self, df):
print('\nInitial DataFrame:')
print(self.df_table)
self.model = PandasModel(df)
self.tableview.setModel(self.model)
self.tableview.show()
# Terminal outputs. # --- DaraFrame output. ---# =================defdataframe_output(self):
print('\nUpdated DataFrame:')
print(self.df_table)
Firstly, when the app is executed:
Initial DataFrame:
x x² x³
0 0.0 0.0 0.0
1 1.0 1.0 1.0
2 2.0 4.0 8.0
3 3.0 9.0 27.0
4 4.0 16.0 64.0
5 5.0 25.0 125.0
After changing some values in the table:
Updated DataFrame:xx²x³0999.00.00.011.01.01.022.04.08.033.09.027.044.016.064.055.025.01111.0
Now I have to work with the DataFrame to make the values of the rows consistent, but that's another question.
Post a Comment for "Synchronize Pandas Dataframe With Pyqt5 Qtableview"