独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)

作者:Parul Pandey

翻译:吴慧聪

校对:吴振东

独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图

简介

提示和技巧总是非常有用的,在编程领域更是如此。有时候,小小的黑科技可以节省你大量的时间和精力。一个小的快捷方式或附加组件有时会是天赐之物,可以成为实用的效率助推器。所以,我在这里介绍下自己编程时最喜欢使用的一些提示和技巧,在这篇文章中汇总起来呈现给大家。有些可能是大家熟悉的,而有些可能是新鲜的,我相信它们会为你下一次处理数据分析的项目时提供便利。

1. 预览Pandas中的数据框数据(Dataframe)

分析预览(profiling)是一个帮助我们理解数据的过程,在Python中Pandas Profiling 是可以完成这个任务的一个工具包,它可以简单快速地对Pandas 数据框进行搜索性数据分析。Pandas中df.describe()df.info()函数通常可以实现EDA过程的第一步,但如果只是给出非常基础的数据预览并不能对分析那些大型的数据集提供帮助。另一方面来看,Pandas Profiling函数能通过一行代码来展示出大量的信息,而在交互式HTML报告中也是这样。

对于一个给定的数据集,Pandas Profiling 工具包将会计算出下面的统计信息:
 
独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图1
由pandas profiling包算出的统计信息

代码示例:

  • 安装

Python2.x的版本中,运用pip或conda安装pandas-profiling资源包:

1
<span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>pip install pandas-profiling<br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">2</span><span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">or</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">3</span>conda install -c anaconda pandas-profiling<br>

  • 使用

现在用一个古老的泰坦尼克数据集来演示多功能python profiler的结果:

1
<span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span><span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">#importing the necessary packages</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">2</span><span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> pandas <span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">as</span> pd <span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">##使用pandas资源包</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">3</span><span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> pandas_profiling <span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">##使用新安装的pandas profiling资源包</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">4</span>df = pd.read_csv( titanic/train.csv ) <span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">##读取数据形成数据框</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">5</span>pandas_profiling.ProfileReport(df) <span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">##使用pandas profiling分析数据</span><br>

这一行就是你需要在jupyter notebook中形成数据分析报告所需的全部代码。这个数据报告十分详细,包括了所有必要的图表。

独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图2
图1.1

这个报告也可以用下面的代码形成交互HTML文件(interactive HTML file)导出:

1
<span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>profile = pandas_profiling.ProfileReport(df)<br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">2</span>profile.to_file(outputfile=<span class="hljs-string" style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">"Titanic data profiling.html"</span>) <span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">##形成Titanic data profiling.html网页</span><br>

独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图3
图1.2

2. Pandas图表(Plot)的交互性

Pandas中有一个内置的.plot()函数作为数据框(Dataframe)的一部分,但因为这个函数呈现的可视化并不是交互的,这使它的功能没那么吸引人。而且,使用pandas.DataFrame.plot()函数绘制图表也并不容易。如果我们想要在没有对代码进行重大修改的情况下用pandas绘制交互式图表要怎么办?嗯,可以通过Cufflinks资源包来帮助你完成这一目的。

Cufflinks资源包将功能强大的plotly和灵活易用的pandas结合,非常便于绘图。现在我们来看看怎么安装和在pandas中使用这个资源包。

代码示例:

  • 安装

Python2.x的版本中,使用pip安装plotly和cufflink:

1
<span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>pip install plotly <span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;"># Plotly is a pre-requisite before installing cufflinks(plotly先于cufflinks安装)</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">2</span>pip install cufflinks<br>

  • 使用

调用方法:

1
<span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span><span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">#importing Pandas </span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">2</span><span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> pandas <span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">as</span> pd <span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">##使用pandas资源包</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">3</span><span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">#importing plotly and cufflinks in offline mode</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">4</span><span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> cufflinks <span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">as</span> cf <span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">##使用cufflinks 和plotly资源包</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">5</span><span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> plotly.offline<br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">6</span>cf.go_offline() <span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">##使用cufflink包中的函数</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">7</span>cf.set_config_file(offline=<span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">False</span>, world_readable=<span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">True</span>)<br>

下面来看一下泰坦尼克数据集所展现的魔力:

1
<span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>df.iplot()<br>

独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图4
图2.1

独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图5
图2.2

右边的可视化展示的是静态的线状图,而左边的图是交互式的,并且更加详细,两个图在代码上没有重大的变化。

Github的链接中将会有更多的示例:
https://github.com/santosjorge/cufflinks/blob/master/Cufflinks%20Tutorial%20-%20Pandas%20Like.ipynb

3. 一点点魔法

Magic命令是Jupyter Notebook中的一组便捷功能,它们旨在解决数据分析中一些常见的问题。你可以用%Ismagic来查阅所有的Magic 命令。
 
独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图6
上图列举了所有可用的Magic 函数

Magic命令有两大类:行magic命令(line magics),以单个% 字符为前缀,单行输入操作;单元magics命令(cell magics),以双%% 字符作为前缀,可以在多行输入操作。如果设置为1,我们使用magic 函数时不需要键入%。

下面让我们来看一下,在常见的数据分析任务中一些可能会用到的命令。

  • % pastebin

% pastebin将代码上传到Pastebin并返回一个链接。Pastebin是一个线上内容托管服务,我们可以在上面存储纯文本,如源代码片段,所形成的链接也可以分享给他人。事实上,Github gist也类似于pastebin,只是它带有版本控制。

代码示例:

来看一下这个file.py的python代码文件中的内容:

1
<span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span><span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">#file.py</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">2</span><span class="hljs-function" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">def</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">foo</span><span class="hljs-params" style="font-size: inherit;line-height: inherit;color: rgb(255, 152, 35);overflow-wrap: inherit !important;word-break: inherit !important;">(x)</span>:</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">3</span>    <span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">return</span> x<br>

在Jupyter Notebook中使用% pastebin形成一个pastebin的链接。

独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图7

  • %matplotlib notebook

%matplotlib inline函数用于在Jupyter笔记本中呈现静态matplotlib图。我们可以尝试用notebook来代替inline得到可轻松地缩放和调整大小的绘图,但要确保在套用matplotlib资源包之前调用该函数。

独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图8
%matplotlib inline vs %matplotlib notebook

  • %run

%run函数用于jupyter notebook中运行一个python脚本文件。

  • %%writefile

%% writefile将执行单元的内容写入文件。下面的这段代码将写入名为foo.py的文件并保存在当前目录中。

独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图9

  • %%latex

%% latex函数将单元格内容以LaTeX的形式呈现。它对于在单元格中编写数学公式和方程很有用。
 
独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图10

4. 发现并减少错误

交互式调试器(interactive debugger)也是一个Magic函数,但我必须给它归个类。如果你在运行代码单元出现异常时,可以在新行中键入%debug运行。这将打开一个交互式调试环境,它将您告诉你代码发生异常的位置。你还可以检查程序中分配的变量值,并在此处执行操作。点击q可退出调试器。

独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图11

5. 输出也可如此美观

如果你想生成美观的数据结构,pprint是首选的模块。它在输出字典数据或JSON数据时特别有用。下面来看一下print 和pprint输出的一个例子:
 
独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图12
独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图13

6. 让提示更突出

可以在你的Jupyter Notebook中使用提示/注释框来突出显示任何重要的内容。注释的颜色取决于指定的提示类型。只需在代码中加入需要突出显示的内容即可。

  • 蓝色提示框:注释

代码示例:

1
2
3
4
5
6
7
8
9
10
11
<span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span><div class="hljs-class" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">
<span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">class</span>="<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">alert</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">alert</span>-<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">block</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">alert</span>-<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">info</span>"&gt;#提示框开头<br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">2</span>&lt;<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">b</span>&gt;<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">Tip</span>: Use blue boxes (alert-info) <span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">for</span> tips <span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">and</span> notes. <br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">3</span>If it’s a note, you don’t have to include the word “Note”.<span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">#提示框内容</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">4</span><span class="hljs-comment" style="font-size: inherit;line-height: inherit;color: rgb(128, 128, 128);overflow-wrap: inherit !important;word-break: inherit !important;">#提示框结尾</span><br><section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="font-size: 15px;"><strong><span style="font-size: 15px;letter-spacing: 1px;"></span></strong></span><br></section><section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="letter-spacing: 1px;font-size: 15px;">输出结果:</span></section><section style="text-align: center;margin-left: 16px;margin-right: 16px;"><img class="rich_pages" data-ratio="0.09444444444444444" data-s="300,640" data-type="png" data-w="720" src="https://img.chuansongme.com/mmbiz_png/heS6wRSHVMnGN1giaQDo4m5It8UTF2iciaTWPa6FNj3Uiabp1ic3gVp9s3CqgWmYhELDDzNxhy8LeWkrGW12nSpJ1Iw/640?wx_fmt=png" style="width: 90%;"></section><section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="letter-spacing: 1px;font-size: 15px;"> </span></section><ul class=" list-paddingleft-2" style="list-style-type: disc;margin-left: 16px;margin-right: 16px;"><li><p style="line-height: 1.5em;"><span style="font-size: 15px;"><strong><span style="font-size: 15px;letter-spacing: 1px;">黄色提示框:警告</span></strong></span></p></li></ul>
<section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="font-size: 15px;"><strong><span style="font-size: 15px;letter-spacing: 1px;"><br></span></strong></span></section><section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="font-size: 15px;"><strong><span style="font-size: 15px;letter-spacing: 1px;">代码示例:</span></strong></span></section><section class="output_wrapper" style='font-size: 16px;color: rgb(62, 62, 62);line-height: 1.6;letter-spacing: 0px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;'><pre style="font-size: inherit;color: inherit;line-height: inherit;"><code class="python language-python hljs" style="margin-right: 2px;margin-left: 2px;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46);padding: 0.5em;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span><div class="hljs-class" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">
<span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">class</span>="<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">alert</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">alert</span>-<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">block</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">alert</span>-<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">warning</span>"&gt;<br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">2</span>&lt;<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">b</span>&gt;<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">Example</span>: Yellow Boxes are generally used to include additional examples <span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">or</span> mathematical formulas.<br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">3</span><br><section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="font-size: 15px;"><strong><span style="font-size: 15px;letter-spacing: 1px;"></span></strong></span><br></section><section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="letter-spacing: 1px;font-size: 15px;">输出结果:</span></section><section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="letter-spacing: 1px;font-size: 15px;"></span></section><section style="text-align: center;margin-left: 16px;margin-right: 16px;"><img class="rich_pages" data-ratio="0.08888888888888889" data-s="300,640" data-type="png" data-w="720" src="https://img.chuansongme.com/mmbiz_png/heS6wRSHVMnGN1giaQDo4m5It8UTF2iciaTjdnCkKWzTPlvJwezmleAEIrclbUf4TmKzRrcaxmyRZ35oyCF1P2Arw/640?wx_fmt=png" style="width: 90%;"></section><section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="font-size: 15px;letter-spacing: 1px;"><br></span></section><ul class=" list-paddingleft-2" style="list-style-type: disc;margin-left: 16px;margin-right: 16px;"><li><p style="line-height: 1.5em;"><span style="font-size: 15px;"><strong><span style="font-size: 15px;letter-spacing: 1px;">绿色提示框:成功</span></strong></span></p></li></ul>
<p style="line-height: 1.5em;"><br></p>
<section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="font-size: 15px;"><strong><span style="font-size: 15px;letter-spacing: 1px;">代码示例:</span></strong></span></section><section class="output_wrapper" style='font-size: 16px;color: rgb(62, 62, 62);line-height: 1.6;letter-spacing: 0px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;'><pre style="font-size: inherit;color: inherit;line-height: inherit;"><code class="python language-python hljs" style="margin-right: 2px;margin-left: 2px;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46);padding: 0.5em;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span><div class="hljs-class" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">
<span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">class</span>="<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">alert</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">alert</span>-<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">block</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">alert</span>-<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">success</span>"&gt;<br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">2</span><span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">Use</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">green</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">box</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">only</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">when</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">necessary</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">like</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">to</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">display</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">links</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">to</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">related</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">content</span>.<br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">3</span><!--<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;"-->div&gt;<br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">4</span><br><section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="font-size: 15px;"><strong><span style="font-size: 15px;letter-spacing: 1px;"></span></strong></span><br></section><section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="letter-spacing: 1px;font-size: 15px;">输出结果:</span></section><section style="text-align: center;margin-left: 16px;margin-right: 16px;"><img class="rich_pages" data-ratio="0.0875" data-s="300,640" data-type="png" data-w="720" src="https://img.chuansongme.com/mmbiz_png/heS6wRSHVMnGN1giaQDo4m5It8UTF2iciaT5N5b2ic22VlLpvjjPeaghubZRfe2NUOragTcs2PHj5xWPaQVjmEuYicA/640?wx_fmt=png" style="width: 90%;"><span style="letter-spacing: 1px;text-align: justify;font-size: 15px;"> </span></section><ul class=" list-paddingleft-2" style="list-style-type: disc;margin-left: 16px;margin-right: 16px;"><li><p style="line-height: 1.5em;"><span style="font-size: 15px;"><strong><span style="font-size: 15px;letter-spacing: 1px;">红色提示框:高危</span></strong></span></p></li></ul>
<p><br></p>
<section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="font-size: 15px;"><strong><span style="font-size: 15px;letter-spacing: 1px;">代码示例:</span></strong></span></section><section class="output_wrapper" style='font-size: 16px;color: rgb(62, 62, 62);line-height: 1.6;letter-spacing: 0px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;'><pre style="font-size: inherit;color: inherit;line-height: inherit;"><code class="python language-python hljs" style="margin-right: 2px;margin-left: 2px;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46);padding: 0.5em;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span><div class="hljs-class" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">
<span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">class</span>="<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">alert</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">alert</span>-<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">block</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">alert</span>-<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">danger</span>"&gt;<br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">2</span><span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">It</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">is</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">good</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">to</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">avoid</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">red</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">boxes</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">but</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">can</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">be</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">used</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">to</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">alert</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">users</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">to</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">not</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">delete</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">some</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">important</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">part</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">of</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">code</span> <span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;">etc</span>. <br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">3</span><!--<span class="hljs-title" style="font-size: inherit;line-height: inherit;color: rgb(165, 218, 45);overflow-wrap: inherit !important;word-break: inherit !important;"-->div&gt;<br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">4</span><br><section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="font-size: 15px;"><strong><span style="font-size: 15px;letter-spacing: 1px;"></span></strong></span><br></section><section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="letter-spacing: 1px;font-size: 15px;">输出结果:</span></section><section style="text-align: center;margin-left: 16px;margin-right: 16px;"><img class="rich_pages" data-ratio="0.08472222222222223" data-s="300,640" data-type="png" data-w="720" src="https://img.chuansongme.com/mmbiz_png/heS6wRSHVMnGN1giaQDo4m5It8UTF2iciaT0icyYic8sMsjKytcUeOT5F5aLXncOCabXhXhEYPJrzYFlcmOOwaKJMyw/640?wx_fmt=png" style="width: 90%;"></section><p><br></p>
<section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="letter-spacing: 1px;font-size: 15px;"></span></section><section style="line-height: 1.5em;text-align: center;margin-left: 16px;margin-right: 16px;"><span style="color: rgb(166, 91, 203);font-size: 15px;"><strong><span style="color: rgb(166, 91, 203);font-size: 15px;letter-spacing: 1px;">7.   输出一个执行单元中的所有结果</span></strong></span></section><section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="letter-spacing: 1px;font-size: 15px;"><br></span></section><section style="line-height: 1.5em;margin-left: 16px;margin-right: 16px;"><span style="letter-spacing: 1px;font-size: 15px;">下面来看一下Jupyter Notebook格中包含的几行代码:</span></section><section class="output_wrapper" style='font-size: 16px;color: rgb(62, 62, 62);line-height: 1.6;letter-spacing: 0px;font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;'><pre style="font-size: inherit;color: inherit;line-height: inherit;"><code class="python language-python hljs" style="margin-right: 2px;margin-left: 2px;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;color: rgb(169, 183, 198);background: rgb(40, 43, 46);padding: 0.5em;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>In[<span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">1</span>]: <span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">10</span>+<span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">5</span>          <br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">2</span>       <span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">11</span>+<span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">6</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">3</span>Out[<span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">1</span>]:  <span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">17</span><br>

通常一个执行单元只输出最后一行的结果,而对于其他输出我们需要添加print()函数。好吧,事实证明我们可以通过在Jupyter Notebook开头添加以下代码来输出每一行的结果:

1
<span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span><span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">from</span> IPython.core.interactiveshell <span class="hljs-keyword" style="font-size: inherit;line-height: inherit;color: rgb(248, 35, 117);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> InteractiveShell  <br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">2</span>InteractiveShell.ast_node_interactivity = <span class="hljs-string" style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">"all"</span><br>

现在所有结果可以被一一输出:

1
<span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>In[<span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">1</span>]: <span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">10</span>+<span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">5</span>          <br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">2</span>       <span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">11</span>+<span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">6</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">3</span>       <span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">12</span>+<span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">7</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">4</span>Out[<span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">1</span>]: <span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">15</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">5</span>Out[<span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">1</span>]: <span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">17</span><br><span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">6</span>Out[<span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">1</span>]: <span class="hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);overflow-wrap: inherit !important;word-break: inherit !important;">19</span><br>

如果要恢复成初始设定:

1
<span class="linenum hljs-number" style="font-size: inherit;line-height: inherit;color: rgb(174, 135, 250);padding-right: 20px;overflow-wrap: inherit !important;word-break: inherit !important;">1</span>InteractiveShell.ast_node_interactivity = <span class="hljs-string" style="font-size: inherit;line-height: inherit;color: rgb(238, 220, 112);overflow-wrap: inherit !important;word-break: inherit !important;">"last_expr"</span><br>

8. 使用‘i’选项运行Python脚本文件

在命令行中运行python脚本的典型方法是:python hello.py。但是,如果在运行相同的脚本文件时额外添加一个 -i,例如python -i hello.py,这会带来更多好处。我们来看看是怎么回事:

首先一旦程序结束,python不会退出编译器。因此,我们可以检查变量的值和程序中定义的函数的正确性。

其次,我们可以轻松地调用python调试器,因为我们仍然在编译器中:
1
<span class="code-snippet_outer" style="font-size: 15px;">import pdb</span>
1
<span class="code-snippet_outer" style="font-size: 15px;">pdb.pm()</span>
这将把我们带到代码发生异常的位置,然后我们可以去处理代码。
 
独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图14

源代码链接:
http://www.bnikolic.co.uk/blog/python-running-cline.html

9. 自动添加代码注释

Ctrl / Cmd + / 命令将自动注释执行单元中的选定行。再次点击组合将取消注释相同的代码行。

10. 删除容易恢复难

你有没有不小心误删过Jupyter Notebook中的执行单元呢?如果有,这里有一个可以撤消该删除操作的快捷方式。

  • 如果你误删了执行单元的内容,可以通过点击CTRL/CMD+Z轻松恢复。

  • 如果你想要恢复所删除执行单元的所有内容,可以点击ESC+Z 或者 EDIT > Undo Delete Cells

总结

在上文中,我列出了在自己在使用Python和Jupyter Notebook时所收集的重要技巧。我相信它们能帮助到你并让你学以致用。到那时我们就可以快乐地写代码啦!

原文标题:
10 Simple hacks to speed upyour Data Analysis in Python
原文链接:
https://towardsdatascience.com/10-simple-hacks-to-speed-up-your-data-analysis-in-python-ec18c6396e6b

推荐阅读
BAT技术大牛推荐:看懂英文文档,每天只需要10分钟做这件事……
【Python实战】北京全年天气状况分析

独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图15

Python的常用包有哪些,分别有什么作用?
程序员常用资源工具集合(建议收藏)
【资源】入门机器学习,照这个课程清单按顺序学就对了
独家 | 10 个简单小窍门带你提高Python数据分析速度(附代码)插图16
喜欢就点击“在看”吧

    已同步到看一看

    发送中

    本站仅按申请收录文章,版权归原作者所有
    如若侵权,请联系本站删除