<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
一些小提示和小技巧可能是非常有用的,特别是在编程领域。有时候使用一点点黑客技术,既可以节省时间,还可能挽救“生命”。
一个小小的快捷方式或附加组件有时真是天赐之物,并且可以成为真正的生产力助推器。所以,这里有一些小提示和小技巧,有些可能是新的,但我相信在下一个数据分析项目中会让你非常方便。
Python学习交流群:1004391443
Pandas中数据框数据的Profiling过程
Profiling(分析器)是一个帮助我们理解数据的过程,而Pandas Profiling是一个Python包,它可以简单快速地对Pandas 的数据框数据进行探索性数据分析。
Pandas中df.describe()和df.info()函数可以实现EDA过程第一步。但是,它们只提供了对数据非常基本的概述,对于大型数据集没有太大帮助。 而Pandas中的Profiling功能简单通过一行代码就能显示大量信息,且在交互式HTML报告中也是如此。
对于给定的数据集,Pandas中的profiling包计算了以下统计信息:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561533523963 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
由Pandas Profiling包计算出的统计信息包括直方图、众数、相关系数、分位数、描述统计量、其他信息——类型、单一变量值、缺失值等。
安装
用pip安装或者用conda安装
<span style="max-width: 1000%;"><span style="max-width: 1000%;">pip</span> <span style="max-width: 1000%;">install pandas-profiling</span></span>
<span style="max-width: 1000%;"> <span style="max-width: 1000%;">conda</span> <span style="max-width: 1000%;">install -c anaconda pandas-profiling</span></span>
用法
下面代码是用很久以前的泰坦尼克数据集来演示多功能Python分析器的结果。
<span style="max-width: 1000%;">#importing the necessary packages</span>
<span style="max-width: 1000%;"> <span style="max-width: 1000%;">import</span> pandas <span style="max-width: 1000%;">as</span> pd</span>
<span style="max-width: 1000%;"> <span style="max-width: 1000%;">import</span> pandas_profiling</span>
<span style="max-width: 1000%;">df = pd.read_csv(<span style="max-width: 1000%;">'titanic/train.csv'</span>)</span>
<span style="max-width: 1000%;"> pandas_profiling.ProfileReport(df)</span>
一行代码就能实现在Jupyter Notebook中显示完整的数据分析报告,该报告非常详细,且包含了必要的图表信息。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561533523970 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
还可以使用以下代码将报告导出到交互式HTML文件中。
<span style="max-width: 1000%;">profile = pandas_profiling.ProfileReport(df)</span>
<span style="max-width: 1000%;">profile.to_file(outputfile=<span style="max-width: 1000%;">"Titanic data profiling.html"</span>)</span>
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561533523973 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
Pandas实现交互式作图
Pandas有一个内置的.plot()函数作为DataFrame类的一部分。但是,使用此功能呈现的可视化不是交互式的,这使得它没那么吸引人。同样,使用pandas.DataFrame.plot()函数绘制图表也不能实现交互。 如果我们需要在不对代码进行重大修改的情况下用Pandas绘制交互式图表怎么办呢?这个时候就可以用Cufflinks库来实现。
Cufflinks库可以将有强大功能的plotly和拥有灵活性的pandas结合在一起,非常便于绘图。下面就来看在pandas中如何安装和使用Cufflinks库。
安装
<span style="max-width: 1000%;">pip <span style="max-width: 1000%;">install</span> plotly</span>
<span style="max-width: 1000%;"># Plotly is a pre-requisite before installing cufflinks</span>
<span style="max-width: 1000%;">pip <span style="max-width: 1000%;">install</span> cufflinks</span>
用法
<span style="max-width: 1000%;">#importing Pandas </span>
<span style="max-width: 1000%;"> <span style="max-width: 1000%;">import</span> pandas <span style="max-width: 1000%;">as</span> pd</span>
<span style="max-width: 1000%;"> <span style="max-width: 1000%;">#importing plotly and cufflinks in offline mode</span></span>
<span style="max-width: 1000%;"> <span style="max-width: 1000%;">import</span> cufflinks <span style="max-width: 1000%;">as</span> cf</span>
<span style="max-width: 1000%;"><span style="max-width: 1000%;">import</span> plotly.offline</span>
<span style="max-width: 1000%;"> cf.go_offline()</span>
<span style="max-width: 1000%;"> cf.set_config_file(offline=<span style="max-width: 1000%;">False</span>, world_readable=<span style="max-width: 1000%;">True</span>)</span>
是时候展示泰坦尼克号数据集的魔力了。
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">df.iplot()
</pre>
<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">df.iplot() vs df.plot()
</pre>
右侧的可视化显示了静态图表,而左侧图表是交互式的,更详细,并且所有这些在语法上都没有任何重大更改。
Magic命令
Magic命令是Jupyter notebook中的一组便捷功能,旨在解决标准数据分析中的一些常见问题。使用命令%lsmagic可以看到所有的可用命令。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561533523987 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
所有可用的Magic命令列表
Magic命令有两种:行magic命令(line magics),以单个%字符为前缀,在单行输入操作;单元magic命令(cell magics),以双%%字符为前缀,可以在多行输入操作。如果设置为1,则不用键入%即可调用Magic函数。
接下来看一些在常见数据分析任务中可能用到的命令:
% pastebin
%pastebin将代码上传到Pastebin并返回url。Pastebin是一个在线内容托管服务,可以存储纯文本,如源代码片段,然后通过url可以与其他人共享。事实上,Github gist也类似于pastebin,只是有版本控制。
在file.py文件中写一个包含以下内容的python脚本,并试着运行看看结果。
<span style="max-width: 1000%;">#file.py</span>
<span style="max-width: 1000%;"> <span style="max-width: 1000%;"><span style="max-width: 1000%;">def</span> <span style="max-width: 1000%;">foo(x)</span>:</span></span>
<span style="max-width: 1000%;"> <span style="max-width: 1000%;">return</span> x</span>
在Jupyter Notebook中使用%pastebin生成一个pastebin url。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561533523991 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
%matplotlib notebook
函数用于在Jupyter notebook中呈现静态matplotlib图。用notebook替换inline,可以轻松获得可缩放和可调整大小的绘图。但记得这个函数要在导入matplotlib库之前调用。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561533523997 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
%run
用%run函数在notebook中运行一个python脚本试试。
<span style="max-width: 1000%;">%run file.py</span>
<span style="max-width: 1000%;">%%writefile</span>
%% writefile是将单元格内容写入文件中。以下代码将脚本写入名为foo.py的文件并保存在当前目录中。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561533524003 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
%%latex
%%latex函数将单元格内容以LaTeX形式呈现。此函数对于在单元格中编写数学公式和方程很有用。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561533524005 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
查找并解决错误
交互式调试器也是一个神奇的功能,我把它单独定义了一类。如果在运行代码单元时出现异常,请在新行中键入%debug并运行它。 这将打开一个交互式调试环境,它能直接定位到发生异常的位置。还可以检查程序中分配的变量值,并在此处执行操作。退出调试器单击q即可。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561533524010 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
Printing也有小技巧
如果您想生成美观的数据结构,pprint是首选。它在打印字典数据或JSON数据时特别有用。接下来看一个使用print和pprint来显示输出的示例。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561533524013 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
让你的笔记脱颖而出
我们可以在您的Jupyter notebook中使用警示框/注释框来突出显示重要内容或其他需要突出的内容。注释的颜色取决于指定的警报类型。只需在需要突出显示的单元格中添加以下任一代码或所有代码即可。
蓝色警示框:信息提示
<span style="max-width: 1000%;"><div <span style="max-width: 1000%;">class</span>=<span style="max-width: 1000%;">"alert alert-block alert-info"</span>></span>
<span style="max-width: 1000%;"> <span style="max-width: 1000%;"><span style="max-width: 1000%;"><<span style="max-width: 1000%;">b</span>></span>Tip:<span style="max-width: 1000%;"></<span style="max-width: 1000%;">b</span>></span></span> Use blue boxes (alert-info) <span style="max-width: 1000%;">for</span> tips and notes. </span>
<span style="max-width: 1000%;"> If it’s a note, you don’t have to include the word “Note”.</span>
<span style="max-width: 1000%;"> <<span style="max-width: 1000%;">/div></span></span>
黄色警示框:警告
<span style="max-width: 1000%;"><div <span style="max-width: 1000%;">class</span>=<span style="max-width: 1000%;">"alert alert-block alert-warning"</span>></span>
<span style="max-width: 1000%;"> <span style="max-width: 1000%;"><span style="max-width: 1000%;"><<span style="max-width: 1000%;">b</span>></span>Example:<span style="max-width: 1000%;"></<span style="max-width: 1000%;">b</span>></span></span> Yellow Boxes are generally used to include additional examples or mathematical formulas.</span>
<span style="max-width: 1000%;"> <<span style="max-width: 1000%;">/div></span></span>
绿色警示框:成功
<span style="max-width: 1000%;"><div <span style="max-width: 1000%;">class</span>=<span style="max-width: 1000%;">"alert alert-block alert-success"</span>></span>
<span style="max-width: 1000%;"> Use green box only when necessary like to display links to related content.</span>
<span style="max-width: 1000%;"> <<span style="max-width: 1000%;">/div></span></span>
红色警示框:高危
<span style="max-width: 1000%;"><div <span style="max-width: 1000%;">class</span>=<span style="max-width: 1000%;">"alert alert-block alert-danger"</span>></span>
<span style="max-width: 1000%;">It is good to avoid red boxes but can be used to alert users to not <span style="max-width: 1000%;">delete</span> some important part <span style="max-width: 1000%;">of</span> code etc. </span>
<span style="max-width: 1000%;"><<span style="max-width: 1000%;">/div></span></span>
打印单元格所有代码的输出结果
假如有一个Jupyter Notebook的单元格,其中包含以下代码行:
<span style="max-width: 1000%;"><span style="max-width: 1000%;">In</span> <span style="max-width: 1000%;">[1]</span>: 10+5 </span>
<span style="max-width: 1000%;"> 11+6</span>
<span style="max-width: 1000%;"><span style="max-width: 1000%;">Out</span> <span style="max-width: 1000%;">[1]</span>: 17</span>
单元格的正常属性是只打印最后一个输出,而对于其他输出,我们需要添加print()函数。然而通过在notebook顶部添加以下代码段可以一次打印所有输出。
添加代码后所有的输出结果就会一个接一个地打印出来。
<span style="max-width: 1000%;"><span style="max-width: 1000%;">In</span> <span style="max-width: 1000%;">[1]</span>: 10+5 </span>
<span style="max-width: 1000%;"> 11+6</span>
<span style="max-width: 1000%;"> 12+7</span>
<span style="max-width: 1000%;"><span style="max-width: 1000%;">Out</span> <span style="max-width: 1000%;">[1]</span>: 15</span>
<span style="max-width: 1000%;"> <span style="max-width: 1000%;">Out</span> <span style="max-width: 1000%;">[1]</span>: 17</span>
<span style="max-width: 1000%;"> <span style="max-width: 1000%;">Out</span> <span style="max-width: 1000%;">[1]</span>: 19</span>
恢复原始设置:
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">InteractiveShell.ast_node_interactivity = "last_expr"
</pre>
使用'i'选项运行python脚本
从命令行运行python脚本的典型方法是:python hello.py。但是,如果在运行相同的脚本时添加-i,例如python -i hello.py,就能提供更多优势。接下来看看结果如何。
首先,即使程序结束,python也不会退出解释器。因此,我们可以检查变量的值和程序中定义的函数的正确性。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561533524026 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
其次,我们可以轻松地调用python调试器,因为我们仍然在解释器中:
<span style="max-width: 1000%;"><span style="max-width: 1000%;">import</span> pdb</span>
<span style="max-width: 1000%;">pdb.pm()</span>
这能定位异常发生的位置,然后我们可以处理异常代码。
自动评论代码
Ctrl / Cmd + /自动注释单元格中的选定行,再次命中组合将取消注释相同的代码行。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561533524032 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
删除容易恢复难
你有没有意外删除过Jupyter notebook中的单元格?如果答案是肯定的,那么可以掌握这个撤消删除操作的快捷方式。
如果您删除了单元格的内容,可以通过按CTRL / CMD + Z轻松恢复它。
如果需要恢复整个已删除的单元格,请按ESC + Z或EDIT>撤消删除单元格。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561533524035 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
结论
在本文中,我列出了使用Python和Jupyter notebook时收集的一些小提示。我相信它们会对你有用,能让你有所收获,从而实现轻松编码!











网友评论