Save Pandas 描述为人类可读性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48158688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save Pandas describe for human readibility
提问by Hrant Davtyan
working on pandas describe function. simple enough code:
在Pandas描述功能上工作。足够简单的代码:
df['Revenue'].describe()
df['收入'].describe()
output is :
输出是:
perfect. my issue is i want to be able to save this data as either a png or a table so that i can place in a single page. this is for my EDA (exploratory data analysis) i have 6 main charts or information that i want to evaluate on each feature. each chart will be a seperate png file. i will then combine into one pdf file. i iterate over 300 + features so doing one at a time is not an option especially seeing as it is done monthly.
完美的。我的问题是我希望能够将此数据保存为 png 或表格,以便我可以放置在单个页面中。这是我的 EDA(探索性数据分析)我有 6 个主要图表或信息,我想对每个功能进行评估。每个图表将是一个单独的 png 文件。然后我将合并成一个 pdf 文件。我迭代了 300 多个功能,所以一次做一个不是一种选择,尤其是看到它是每月完成的。
if you know of a way to save this table as a png or other similar file format that would be great. thanks for the look
如果您知道将此表另存为 png 或其他类似文件格式的方法,那就太好了。谢谢你的样子
回答by Hrant Davtyan
Saving as a csv or xlsx file
另存为 csv 或 xlsx 文件
You may use to_csv("filename.csv")or to_excel("filename.xlsx")methods to save the file in a comma separated format and then manipulate/format it in Excel however you want. Example:
您可以使用to_csv("filename.csv")或to_excel("filename.xlsx")方法以逗号分隔格式保存文件,然后根据需要在 Excel 中对其进行操作/格式化。例子:
df['Revenue'].describe().to_csv("my_description.csv")
Saving as a png file
保存为 png 文件
As mentioned in the comments, thispost explains how to save pandas dataframe to png file via matplot lib. In your case, this should work:
正如评论中提到的,这篇文章解释了如何通过 matplot lib 将 Pandas 数据帧保存到 png 文件。在您的情况下,这应该有效:
import matplotlib.pyplot as plt
from pandas.plotting import table
desc = df['Revenue'].describe()
#create a subplot without frame
plot = plt.subplot(111, frame_on=False)
#remove axis
plot.xaxis.set_visible(False)
plot.yaxis.set_visible(False)
#create the table plot and position it in the upper left corner
table(plot, desc,loc='upper right')
#save the plot as a png file
plt.savefig('desc_plot.png')