10个Python进行数据分析的小技巧_python 怎么分析实验数据,2024年最新4个改变你编程技能的小技巧

60 阅读6分钟
pip install pandas-profiling conda install -c anaconda pandas-profiling

用法

下面代码是用很久以前的泰坦尼克数据集来演示多功能Python分析器的结果。

#importing the necessary packages 
import pandas as pd 
import pandas_profiling
df = pd.read\_csv('titanic/train.csv') 
pandas_profiling.ProfileReport(df)

一行代码就能实现在Jupyter Notebook中显示完整的数据分析报告,该报告非常详细,且包含了必要的图表信息。
在这里插入图片描述
还可以使用以下代码将报告导出到交互式HTML文件中。

profile = pandas_profiling.ProfileReport(df)
profile.to\_file(outputfile="Titanic data profiling.html")

在这里插入图片描述

Pandas实现交互式作图

Pandas有一个内置的.plot()函数作为DataFrame类的一部分。但是,使用此功能呈现的可视化不是交互式的,这使得它没那么吸引人。同样,使用pandas.DataFrame.plot()函数绘制图表也不能实现交互。如果我们需要在不对代码进行重大修改的情况下用Pandas绘制交互式图表怎么办呢?这个时候就可以用Cufflinks库来实现。

Cufflinks库可以将有强大功能的plotly和拥有灵活性的pandas结合在一起,非常便于绘图。下面就来看在pandas中如何安装和使用Cufflinks库。

安装

pip install plotly
# Plotly is a pre-requisite before installing cufflinks
pip install cufflinks

用法

#importing Pandas 
import pandas as pd 
#importing plotly and cufflinks in offline mode 
import cufflinks as cfimport 
plotly.offline cf.go\_offline() 
cf.set\_config\_file(offline=False, world_readable=True)

是时候展示泰坦尼克号数据集的魔力了。

df.iplot()

在这里插入图片描述

df.iplot() vs df.plot()

右侧的可视化显示了静态图表,而左侧图表是交互式的,更详细,并且所有这些在语法上都没有任何重大更改。

Magic命令

Magic命令是Jupyter notebook中的一组便捷功能,旨在解决标准数据分析中的一些常见问题。使用命令%lsmagic可以看到所有的可用命令。
在这里插入图片描述
所有可用的Magic命令列表

Magic命令有两种:行magic命令(line magics),以单个%字符为前缀,在单行输入操作;单元magic命令(cell magics),以双%%字符为前缀,可以在多行输入操作。如果设置为1,则不用键入%即可调用Magic函数。

接下来看一些在常见数据分析任务中可能用到的命令:

% pastebin

%pastebin将代码上传到Pastebin并返回url。Pastebin是一个在线内容托管服务,可以存储纯文本,如源代码片段,然后通过url可以与其他人共享。事实上,Github gist也类似于pastebin,只是有版本控制。

在file.py文件中写一个包含以下内容的python脚本,并试着运行看看结果。

#file.py 
def foo(x):     
    return x

在Jupyter Notebook中使用%pastebin生成一个pastebin url。
在这里插入图片描述

%matplotlib notebook

函数用于在Jupyter notebook中呈现静态matplotlib图。用notebook替换inline,可以轻松获得可缩放和可调整大小的绘图。但记得这个函数要在导入matplotlib库之前调用。
在这里插入图片描述

%run

用%run函数在notebook中运行一个python脚本试试。

%run file.py

%%writefile

%% writefile是将单元格内容写入文件中。以下代码将脚本写入名为foo.py的文件并保存在当前目录中。
在这里插入图片描述

%%latex

%%latex函数将单元格内容以LaTeX形式呈现。此函数对于在单元格中编写数学公式和方程很有用。
在这里插入图片描述

查找并解决错误

交互式调试器也是一个神奇的功能,我把它单独定义了一类。如果在运行代码单元时出现异常,请在新行中键入%debug并运行它。这将打开一个交互式调试环境,它能直接定位到发生异常的位置。还可以检查程序中分配的变量值,并在此处执行操作。退出调试器单击q即可。
在这里插入图片描述

Printing也有小技巧

如果您想生成美观的数据结构,pprint是首选。它在打印字典数据或JSON数据时特别有用。接下来看一个使用print和pprint来显示输出的示例。
在这里插入图片描述

让你的笔记脱颖而出

我们可以在您的Jupyter notebook中使用警示框/注释框来突出显示重要内容或其他需要突出的内容。注释的颜色取决于指定的警报类型。只需在需要突出显示的单元格中添加以下任一代码或所有代码即可。

蓝色警示框:信息提示

<div class="alert alert-block alert-info"> 
<b>Tip:</b> Use blue boxes (alert-info) for tips and notes.  If it’s a note, you don’t have to include the word “Note”. </div>

在这里插入图片描述

黄色警示框:警告

<div class="alert alert-block alert-warning"> 
<b>Example:</b> Yellow Boxes are generally used to include additional examples or mathematical formulas. </div>

在这里插入图片描述

绿色警示框:成功

<div class="alert alert-block alert-success">
Use green box only when necessary like to display links to related content. </div>

在这里插入图片描述

红色警示框:高危

<div class="alert alert-block alert-danger">
It is good to avoid red boxes but can be used to alert users to not delete some important part of code etc. </div>

在这里插入图片描述

打印单元格所有代码的输出结果

假如有一个Jupyter Notebook的单元格,其中包含以下代码行:

In  [1]: 10+5                    
        11+6

Out [1]: 17

单元格的正常属性是只打印最后一个输出,而对于其他输出,我们需要添加print()函数。然而通过在notebook顶部添加以下代码段可以一次打印所有输出。

添加代码后所有的输出结果就会一个接一个地打印出来。

In  [1]: 10+5                   


![img](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/dd54146241bb4c4c9e8e9e4c76f4011f~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3MzIxMjA3NDIwNDUy:q75.awebp?rk3s=f64ab15b&x-expires=1771737793&x-signature=DBOepsCnTLrc71onu5CgI9Z3WGI%3D)
![img](https://p9-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/b509a3ec5ba94e1884047d1dd0016e81~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3MzIxMjA3NDIwNDUy:q75.awebp?rk3s=f64ab15b&x-expires=1771737793&x-signature=XRTs3Ft90qfD3oXZqDg%2FCF77H8w%3D)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**了解详情》docs.qq.com/doc/DSlVlZExWQ0FRSE9H**