如何将数据从QTableWidget检索到Dataframe? [英] How can I retrieve data from a QTableWidget to Dataframe?

查看:159
本文介绍了如何将数据从QTableWidget检索到Dataframe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处于可编辑模式的QTableWidget,用户可以在其中输入整数输入,如何生成在此表中输入的数据列表以对其执行操作,这是我的手动代码:

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

此外,我正在使用以下代码生成QTableWidget:

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)

我的规格是:pandas 0.18.1,PyQt 4和Python 2.7

My specs are : pandas 0.18.1 , PyQt 4 and Python 2.7

推荐答案

我认为您在更新/联接方面使它有些复杂.最简单的方法是先创建完整尺寸的DataFrame(用NaN填充),然后将数据分配给该尺寸:

I think you're overcomplicating it a little with the updates/joins. The simplest approach is to create the full-size DataFrame first (filled with NaN) and then assign the data to this:

def dataframe_generation_from_table(self,table):
    number_of_rows = table.rowCount()
    number_of_columns = table.columnCount()

    tmp_df = pd.DataFrame( 
                columns=['Date', str(self.final_lvl_of_analysis), 'Value'], # Fill columnets
                index=range(number_of_rows) # Fill rows
                ) 

    for i in range(number_of_rows):
        for j in range(number_of_columns):
            tmp_df.ix[i, j] = table.item(i, j).data()

    return tmp_df

上面的代码通过数字索引将数据分配到其位置,因此QtTableWidget中的位置1,1将最终在DataFrame中的1,1处结束.这样,您在移动数据时无需担心列标题.如果要更改列名,可以在创建DataFrame时进行此操作,更改传递给columns=参数的值.

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 the DataFrame. 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 the DataFrame, changing the values passed into the columns= parameter.

如果要将列更改为DateTime格式,则应该可以在循环后执行以下操作:

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:

tmp_df['Date'] = pd.to_datetime( tmp_df['Date'] )

这篇关于如何将数据从QTableWidget检索到Dataframe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆