I have a QTableWidget in editable mode in which user puts in integer input , how can I generate a list of data entered in this table so as to perform operations on it , here is my manual code for that:
def dataframe_generation_from_table(self,table):
number_of_rows = table.rowCount()
number_of_columns = table.columnCount()
tmp_df = pd.DataFrame({ 'Date' : [] , str(self.final_lvl_of_analysis) :[], 'Value': []})
for i in range(0,number_of_rows):
for j in range(0,number_of_columns):
tmp_item = table.item(i,j)
tmp_df2 = pd.DataFrame( { 'Date' : [pd.to_datetime(table.horizontalHeaderItem(j).data())] , str(self.final_lvl_of_analysis) :[ str(table.verticalHeaderItem(i).data())], 'Value': [float(tmp_item.data(0))]})
print tmp_df2
tmp_df.update(tmp_df2, join = 'left', overwrite = False)
return tmp_df
Also , I am using the following code for QTableWidget generation:
self.pd_table = QtGui.QTableWidget(self.groupBox_19)
self.pd_table.setObjectName(_fromUtf8("pd_table"))
self.pd_table.setColumnCount(0)
self.pd_table.setRowCount(0)
My specs are : pandas 0.18.1 , PyQt 4 and Python 2.7
The change from
.data()
to.text()
eliminated theValueError
.I think you're overcomplicating it a little with the updates/joins. The simplest approach is to create the full-size
DataFrame
first (filled withNaN
) and then assign the data to this:The above code assigns data to it's location by the numerical index, so position 1,1 in the
QtTableWidget
will end up at 1,1 in theDataFrame
. This way you don't need to worry about the column headers when moving data. If you want to change the column names you can do that when creating theDataFrame
, changing the values passed into thecolumns=
parameter.If you want to change a column to
DateTime
format, you should be able to do this in a single operation after the loop with: