简体   繁体   中英

Creating a table from a panda dataframe

I currently have a python script that analyzes a jstack dump and outputs a dataframe like this:

数据框

I want to turn this into a png or jpg image of a table containing this data. Something simple like this:

桌子

Does anyone know what the easiest, most straight forward way to produce an image like this so it is saved in the same path that I am running the code?

Thank you! Javier

***EDIT:

The provided solutions are outputting an unreadable table with mostly white space as below:

Table output

Can anyone suggest what I'm doing wrong and which parameters to adjust?

***EDIT #2:

Here is my code:

df = pd.DataFrame(table_data, columns = ["Method Name", "# of threads", "% of threads"])

ax = plt.subplot(111, frame_on=False)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)

t = table(ax, df)
t.auto_set_font_size(False)
t.set_fontsize(12)
fig.savefig("test.png")

And the current output:

Table output #2

***EDIT #3:

This is the dataframe I am passing:

                                         Method Name # of threads % of threads
0  at sun.nio.ch.EPollArrayWrapper.epollWait(Nati...           33        32.35
1             at sun.misc.Unsafe.park(Native Method)           29        28.43
2                                NO JAVA STACK TRACE           18        17.64
3  at java.net.PlainSocketImpl.socketAccept(Nativ...            6        05.88
4  at xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...            5        04.90
5            at java.lang.Object.wait(Native Method)            4        03.92
6           at java.lang.Thread.sleep(Native Method)            3        02.94
7  at java.net.SocketInputStream.socketRead0(Nati...            3        02.94
8  at sun.nio.ch.ServerSocketChannelImpl.accept0(...            1        00.98

Can you try this?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.axis('off')
ax.axis('tight')
t= ax.table(cellText=df.values, colWidths = [0.9]*len(df.columns),  colLabels=df.columns,  loc='center')
t.auto_set_font_size(False) 
t.set_fontsize(8)
fig.tight_layout()
plt.show()

This is the output i got: You can change the 0.9 of the column width to resize the width of the column and change the font if it is too small在此处输入图片说明 You an plot pandas table using this.

from pandas.plotting import table
table(df)

You can hide all the axis and frames by using below code

ax = plt.subplot(111, frame_on=False)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
table(ax, df)

You can save the table using the savefig.

plt.savefig('table.png')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM