Pandas:如何将cProfile输出存储在pandas DataFrame中?

问题描述 投票:9回答:3

[已经有一些文章讨论了使用cProfile进行python分析,以及由于以下示例代码中的输出文件restats不是纯文本文件而对输出进行分析的挑战。下面的摘录只是docs.python.org/2/library/profile中的一个示例,不能直接复制。

import cProfile
import re
cProfile.run('re.compile("foo|bar")', 'restats')

这里有一个讨论:Profile a python script using cProfile into an external filedocs.python.org上有更多有关如何使用pstats.Stats分析输出的详细信息(仍然只是一个示例,并且不可重现:]

import pstats
p = pstats.Stats('restats')
p.strip_dirs().sort_stats(-1).print_stats()

我可能在这里遗漏了一些非常重要的细节,但是我真的很想将输出存储在pandas DataFrame中,然后从那里做进一步的分析。

我认为这将非常简单,因为运行cProfile.run()的iPython中的输出看起来很整洁:

In[]:
cProfile.run('re.compile("foo|bar")'

Out[]:

enter image description here

关于如何以相同格式将其放入pandas DataFrame的任何建议?

python pandas profiling
3个回答
4
投票

[https://github.com/ssanderson/pstats-view看起来可能会做您想要的(尽管与可视化数据并使之具有交互性有关的不必要依赖性:]


3
投票

我知道这已经有了一个答案,但是对于任何不想下载另一个模块的麻烦的人来说,这是一个准备就绪的粗略脚本,应将其关闭:


1
投票

如果您在cmd中使用python -m cProfile your_script.py

© www.soinside.com 2019 - 2024. All rights reserved.