1. 快捷键
在jupyter notebook菜单栏有Help按钮,能够查看jupyter的快捷键node
2. 将多个变量输出
通常jupyter notebook默认只打印最后一个变量的结果。好比python
from pydataset import dataquakes = data('quakes')quakes.head(10) #前10行数据quakes.tail(3) #后3行数据
web
shell
浏览器
微信
经过设置InteractiveShell.astnodeinteractivity参数为all,就可让全部的变量或者声明都能显示出来
markdown
from IPython.core.interactiveshell import InteractiveShellInteractiveShell.ast_node_interactivity = 'all'
app
机器学习
from pydataset import dataquakes = data('quakes')quakes.head(10) #前10行数据quakes.tail(3) #后3行数据
函数
3. 问号?
除了Help菜单能让咱们快读查看numpy、pandas、scipy和matplotlib库,其实在cell中使用 ?
能够查看库、函数、方法和变量的信息。
#查看库的信息import os?os
#查看函数信息?print()
#查看变量信息a = [1,2,3,4]?a
4. 在notebook中画图
做图最经常使用的就是matplotlib,记得在cell中写上这句
%matplotlib inline
%matplotlib inlineimport pandas as pdseries = pd.Series([1,3,5,6,2])series.plot(kind='pie')
5. IPython魔法命令
查看当前工做目录
%pwd
执行上面的代码,获得
'/Users/suosuo/Desktop/20180820 jupyter notebook技巧'
更改当前工做目录
#更改当前工做目录%cd /Users/suosuo/Desktop
查看目录文件列表
#查看目录文件列表%ls /Users/suosuo/Desktop/用python文本分析
执行上面的代码,获得
01-configuration.zip 02-base.zip 03-crawler.zip 04-textprocess.zip 05-textprocess.zip
写入文件
#写入文件,向test.py中写入print('测试%%writefile魔法')%%writefile test.pyprint('测试%%writefile魔法')
执行上面的代码,获得
Writing test.py
运行脚本
#运行脚本%run test.py
执行上面的代码,获得
测试%%writefile魔法
查看当前变量
#查看当前变量a = 1b = [1,2,3,4]%whos
执行上面的代码,获得
Variable Type Data/Info -------------------------------- a int 1 b list n=4 pd module <module 'pandas' from '/L<...>ages/pandas/__init__.py'> s NoneType None series Series 0 1\n1 3\n2 5\n3<...> 6\n4 2\ndtype: int64
清除所有变量
#清除所有变量a = 1b = [1,2,3,4]%reset
执行上面的代码,获得
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
测试单行运行时间
#测试单行运行时间%timeit x = [i**2 for i in range(10000)]%timeit y = [i**2 for i in x]
执行上面的代码,获得
5.16 ms ± 215 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)4.82 ms ± 190 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
6. 执行shell命令
命令行的命令前面加个 !
便可在notebook中进行。
好比咱们想要安装jieba库,须要打开终端输入
pip3 install jieba
如今,咱们能够在notebook中输入下面命令安装jieba
!pip3 install jieba
Collecting jieba[?25l Downloading https://files.pythonhosted.org/packages/71/46/c6f9179f73b818d5827202ad1c4a94e371a29473b7f043b736b4dab6b8cd/jieba-0.39.zip (7.3MB)[K 100% |████████████████████████████████| 7.3MB 284kB/s ta 0:00:01[?25hInstalling collected packages: jieba Running setup.py install for jieba ... [?25ldone[?25hSuccessfully installed jieba-0.39
7. markdown标记语言
markdown语法 | 做用 |
---|---|
# | 有几个#就是几级标题 |
** | 两对**夹住的内容变为斜体 |
- | 无序列表 |
一级标题
# 一级标题
二级标题
## 二级标题
三级标题
### 三级标题
有序列表
元素1
元素2
元素3
有序列表1. 元素12. 元素23. 元素3
无序列表
元素1
元素2
元素3
无序列表- 元素1- 元素2- 元素3
函数 | 做用 |
---|---|
print() | 打印 |
help() | 查看帮助文档 |
|函数|做用||---|---||print()|打印||help()|查看帮助文档|
8. 使用LaTex写公式
当咱们在markdown编辑模式下输入
$P(A|B)=\frac{P(B|A)P(A)}{P(B)}$
会被MathJax渲染成
import requests?requests.get()
9. 为jupyter扩展插件
执行下面操做
!pip3 install jupyter_contrib_nbextensions!jupyter contrib nbextension install !jupyter_contrib_nbextensions
咱们的jupyter notebook发生的了变化,以下图所示,多了nbextensions
而在.ipynb文件中增长了下图的这个按钮,点击该按钮咱们就可使用jupyter的展现功能(浏览器PPT功能)
!pip3 install jupyter_contrib_nbextensions!jupyter contrib nbextension install !jupyter_contrib_nbextensions
往期文章
本文分享自微信公众号 - 大邓和他的Python(DaDengAndHisPython)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。