如何将 pandas DataFrame表另存为PNG [英] How to save a pandas DataFrame table as a png

查看:222
本文介绍了如何将 pandas DataFrame表另存为PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个结果熊猫数据框.该数据帧充当表格.有MultiIndexed列,每行代表一个名称,即创建DataFrame时的index=['name1','name2',...].我想显示此表并将其另存为png(或其他任何图形格式).目前,我能得到的最接近的是将其转换为html,但是我想要一个png.似乎已经问过类似的问题,例如如何将Pandas数据框/系列数据保存为图形?

I constructed a pandas dataframe of results. This data frame acts as a table. There are MultiIndexed columns and each row represents a name, ie index=['name1','name2',...] when creating the DataFrame. I would like to display this table and save it as a png (or any graphic format really). At the moment, the closest I can get is converting it to html, but I would like a png. It looks like similar questions have been asked such as How to save the Pandas dataframe/series data as a figure?

但是,标记的解决方案将数据帧转换为折线图(而不是表格),而另一种解决方案则依赖于PySide,我只是想避免这一点,因为我无法在Linux上pip安装它.我希望此代码易于移植.我真的期望使用python可以轻松地将表创建为png.感谢所有帮助.

However, the marked solution converts the dataframe into a line plot (not a table) and the other solution relies on PySide which I would like to stay away simply because I cannot pip install it on linux. I would like this code to be easily portable. I really was expecting table creation to png to be easy with python. All help is appreciated.

推荐答案

Pandas允许您使用matplotlib绘制表(详细信息

Pandas allows you to plot tables using matplotlib (details here). Usually this plots the table directly onto a plot (with axes and everything) which is not what you want. However, these can be removed first:

import matplotlib.pyplot as plt
import pandas as pd
from pandas.table.plotting import table # EDIT: see deprecation warnings below

ax = plt.subplot(111, frame_on=False) # no visible frame
ax.xaxis.set_visible(False)  # hide the x axis
ax.yaxis.set_visible(False)  # hide the y axis

table(ax, df)  # where df is your data frame

plt.savefig('mytable.png')

输出可能不是最漂亮的,但是您可以找到table()函数的其他参数此处. 也要感谢这篇文章有关如何在matplotlib中删除轴的信息.

The output might not be the prettiest but you can find additional arguments for the table() function here. Also thanks to this post for info on how to remove axes in matplotlib.

这是使用上述方法进行绘图时模拟多索引的一种方式(诚然是很hack的).如果您有一个称为df的多索引数据框,如下所示:

Here is a (admittedly quite hacky) way of simulating multi-indexes when plotting using the method above. If you have a multi-index data frame called df that looks like:

first  second
bar    one       1.991802
       two       0.403415
baz    one      -1.024986
       two      -0.522366
foo    one       0.350297
       two      -0.444106
qux    one      -0.472536
       two       0.999393
dtype: float64

首先重置索引,使它们成为普通列

First reset the indexes so they become normal columns

df = df.reset_index() 
df
    first second       0
0   bar    one  1.991802
1   bar    two  0.403415
2   baz    one -1.024986
3   baz    two -0.522366
4   foo    one  0.350297
5   foo    two -0.444106
6   qux    one -0.472536
7   qux    two  0.999393

通过将它们设置为空字符串,从高阶多索引列中删除所有重复项(在我的示例中,我在"first"中仅具有重复索引):

Remove all duplicates from the higher order multi-index columns by setting them to an empty string (in my example I only have duplicate indexes in "first"):

df.ix[df.duplicated('first') , 'first'] = '' # see deprecation warnings below
df
  first second         0
0   bar    one  1.991802
1          two  0.403415
2   baz    one -1.024986
3          two -0.522366
4   foo    one  0.350297
5          two -0.444106
6   qux    one -0.472536
7          two  0.999393

将索引"上的列名称更改为空字符串

Change the column names over your "indexes" to the empty string

new_cols = df.columns.values
new_cols[:2] = '',''  # since my index columns are the two left-most on the table
df.columns = new_cols 

现在调用表格函数,但将表格中的所有行标签设置为空字符串(这可确保不显示绘图的实际索引):

Now call the table function but set all the row labels in the table to the empty string (this makes sure the actual indexes of your plot are not displayed):

table(ax, df, rowLabels=['']*df.shape[0], loc='center')

等一下:

您不太漂亮但功能齐全的多索引表.

Your not-so-pretty but totally functional multi-indexed table.

正如评论中指出的,table的导入语句:

As pointed out in the comments, the import statement for table:

from pandas.tools.plotting import table

现在在较新版本的熊猫中已弃用,而推荐使用:

is now deprecated in newer versions of pandas in favour of:

from pandas.plotting import table 

弃用警告2

ix索引器现已完全已弃用,因此我们应改用loc索引器.替换:

DEPRECATION WARNINGS 2

The ix indexer has now been fully deprecated so we should use the loc indexer instead. Replace:

df.ix[df.duplicated('first') , 'first'] = ''

df.loc[df.duplicated('first') , 'first'] = ''

这篇关于如何将 pandas DataFrame表另存为PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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