【DSW Gallery】Introduction to Jupyter

Introduction
JupyterNotebook is a Python environment for writing Jupyter Notebooks. In addition to regular Python syntax, Jupyter Notebook also supports Markdown syntax, as well as some special syntax, such as tables and diagrams. This article introduces some common usage skills of Jupyter Notebook, hoping to improve the development experience of developers in the process of using Jupyter Notebook
prerequisite
Because the DSW instance you create always has a Jupyter Notebook environment, you can directly run the code in this article
1. Execute Shell command
Notebook is a new command line, and commands beginning with "!" will be executed as command lines
# current directory file list
!ls
# current path
!pwd
# echo command
!echo 'Hello World'
_html meta.json
demo.csv to_be_loaded_by_magic_command.py
jupyterIntroduction.ipynb
/Users/chenyi/IdeaProjects/pai-dsw-examples/introduction/jupyter/jupyterIntroduction
hello world
# You can assign the execution result of the command line to a variable and interact with python
files = !ls
print(files)
directory = !pwd
print(directory)
# Note that the results returned by the command line
2. Multiple outputs in one cell
By default, only the code result of the last line in the cell will be output; by setting, multiple contents can be output in one cell
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
import pandas as pd
demoDf = pd.read_csv('demo.csv')
demoDf. head()
demoDf.shape
demoDf['Year']. value_counts()
3. View all variables and variable values in the current notebook
# %who will output the variables that already exist in the current notebook
%who
# %whos In addition to the output variable, the type and current value of the variable will also be displayed
%whos
4. Running time statistics
time, and timeit two magic commands, the following introduces the use of cell mode
%%time
# In cell mode, count the time it takes for the code in the entire cell to run once
import pandas as pd
demoDf = pd.read_csv('demo.csv')
demoDf. head()
demoDf.shape
demoDf['Year']. value_counts()
%%timeit
# In unit mode, execute the code block several times and calculate the average time. Results are given as mean +/- std
import pandas as pd
demoDf = pd.read_csv('demo.csv')
demoDf. head()
demoDf.shape
demoDf['Year']. value_counts()
5. Magic Commands
magiccommand is a special command that helps increase productivity
# built-in magic commands
%ls magic
# Print the current directory, pay attention to distinguish it from the shell command, some command functions overlap
%pwd
#Change the current directory
%cd /home/admin/workspace/pai-dsw-examples/introduction/jupyter/jupyterIntroduction
# Display current working directory files
%ls
# Load the specified file code
%load 'to_be_loaded_by_magic_command.py'
6. Use shortcut keys
6.1 Display Documentation
If you forget how to use a method or parameter, you can use shift + tab to display the document
6.2 Split cells
Select multiple cells according to shift, shift + m can merge multiple cells

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us