I have a two column qtablewidget that allows the user to add and delete rows through push buttons. I'd like to be able to read the contents of the table into a dataframe in order to count certain strings that are in the table. Here's an example of the table in the gui.
I want to read that table into a dataframe in order to analyze it. Here's the code I've tried so far:
def dataframe_gen(self, table):
num_rows = table.rowCount()
num_cols = table.columnCount()
tmp_df = pd.DataFrame(
columns = ['At_Bat_Num', 'Event'], index=range(num_rows))
for i in range(num_rows):
for j in range(num_cols):
tmp_df.ix[i, j] = table.item(i, j).data()
I believe I need to store it into a dataframe before I can analyze it, as it only exists as the QTableWidget. Values are added and deleted from the gui.
Code:
def dataframe_gen(self):
num_rows = self.tableWidget_Events.rowCount()
num_cols = self.tableWidget_Events.columnCount()
tmp_df = pd.DataFrame(
columns = ['At_Bat_Num', 'Event'], index=range(num_rows))
for i in range(num_rows):
for j in range(num_cols):
tmp_df.ix[i, j] = self.tableWidget_Events.item(i, j)
print(tmp_df)
Output:
At_Bat_Num \
0 <PyQt5.QtWidgets.QTableWidgetItem object at 0x...
Event
0 <PyQt5.QtWidgets.QTableWidgetItem object at 0x...