MB4ig
MB4ig

Reputation: 65

Outputting the results of a DataFrame to a QLabel

I have a Pandas DataFrame that outputs correctly in the Python console, however I'm creating an application using QT designer and would like this DataFrame to be output on a QLabel, the label is called:

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

Do I need to use another widget type to display the DataFrame?

The code I have is:

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)

The error is:

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

Please can you assist. Thanks.

Upvotes: 1

Views: 389

Answers (1)

eyllanesc
eyllanesc

Reputation: 244212

As the error points out, QLabel does not expect a dataframe but a string, so you must pass the result of 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()

Upvotes: 1

Related Questions