【问题标题】:Outputting the results of a DataFrame to a QLabel将 DataFrame 的结果输出到 QLabel
【发布时间】:2020-01-21 15:30:46
【问题描述】:

我有一个可以在 Python 控制台中正确输出的 Pandas DataFrame,但是我正在使用 QT 设计器创建一个应用程序,并希望将此 DataFrame 输出到 QLabel 上,标签称为:

<widget class="QLabel" name="LBL_RESULT">. 

我是否需要使用其他小部件类型来显示 DataFrame?

我的代码是:

df=pd.DataFrame({"Identifier":id,"Description":desc})
print(df.to_string()) # prints everything to screen, not just the first page and last page.
df.to_csv('Test.txt',index=False) # saves the DataFrame to a text file
self.LBL_RESULT.setText(df)

错误是:

self.LBL_RESULT.setText(df)
TypeError: setText(self, str): argument 1 has unexpected type 'DataFrame'

请您帮忙。谢谢。

【问题讨论】:

    标签: python python-3.x pandas pyqt qlabel


    【解决方案1】:

    正如错误指出的那样,QLabel 不期望数据帧而是字符串,因此您必须传递to_string() 的结果:

    df = pd.DataFrame({"Identifier":id,"Description":desc})
    df.to_csv('Test.txt',index=False) # saves the DataFrame to a text file
    self.LBL_RESULT.setText(df.to_string())
    self.LBL_RESULT.adjustSize()

    【讨论】:

    • 非常感谢!非常感谢快速响应,这非常有效。感谢您的解释:)
    猜你喜欢
    • 2020-12-07
    • 2020-06-15
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 2018-01-02
    相关资源
    最近更新 更多