0

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. enter image description here

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...  
Justin
  • 139
  • 1
  • 3
  • 11
  • 1
    What label do you mean? – eyllanesc Apr 30 '18 at 02:01
  • Just a basic label that will take on the value of the count of a string. So a label saying there are 3 "a" items. – Justin Apr 30 '18 at 02:33
  • and what does the QTableWidget have to do with that topic? The code that you sample is not relevant. – eyllanesc Apr 30 '18 at 02:36
  • The table is a QTableWidget. It doesn't exist outside of that. It's created only from the gui. I've edited the post to reflect that. – Justin Apr 30 '18 at 02:47
  • The question is duplicated because your goal is to calculate the frequency of the elements, and establish it in some QLabel, the first task has many solutions that are shown in the link, and the second task is trivial. – eyllanesc Apr 30 '18 at 02:53
  • I disagree. The first task is getting the table into a pandas dataframe. Tha's what I need help with, not the rest, as I stated in the question. – Justin Apr 30 '18 at 02:57
  • Read this: https://stackoverflow.com/questions/37680981/how-can-i-retrieve-data-from-a-qtablewidget – eyllanesc Apr 30 '18 at 02:57
  • That's what the code above is based on. Except it doesn't seem to be working. To try and correct it, I'm just trying to print out each item in the table before I store them. Right now, all that's being printed is this: – Justin Apr 30 '18 at 03:49
  • execute `print(tmp_df)` after loops – eyllanesc Apr 30 '18 at 03:55
  • I've attached the code I ran along with the results from the print statement. – Justin Apr 30 '18 at 04:06
  • change `tmp_df.ix[i, j] = self.tableWidget_Events.item(i, j)` to `tmp_df.ix[i, j] = self.tableWidget_Events.item(i, j).text()` – eyllanesc Apr 30 '18 at 04:12
  • Perfect! Thank you. – Justin Apr 30 '18 at 04:18

0 Answers0