If you want to do data analysis in Python, Jupyter Notebook is one of the tools you should be familiar with, and there are a lot of handy tricks for saving time. In this article, I’ll share some of the things I’m used to doing with Notebook!
1. Install the third-party library in the Notebook
Sometimes we need to temporarily install a third-party library while doing data analysis or using some online/remote Notebook, and if we restart the Notebook after installing it from the command line (or even without a command line interface), all progress will be lost!
This is when we can use it! PIP install XXX can install third-party libraries, such as Pyecharts, directly on notebook
In fact! You can also use the same symbol that executes shell statements in the notebook. To perform! Ls and other arbitrary command line code.
2. Use markdown in Notebook
When the.ipynb file opens, all the explanatory text is annotated with #, which is a pain to read.
In the Notebook, you can use markDown statements to write text, type formulas, and paste images. Just select the target cell, press ESC, then M, or label the current cell from the menu bar
3. Calculate the running time quickly
Sometimes we need to calculate the running time of a function or procedure to measure the efficiency of the code. In other ides, we might need to write a function or use a third-party module to do this
%time: The time it takes for code to run once in line mode
%%time: The time it takes for code to run once in cell mode
%timeit: Executes the code block several times in line mode for the best result
%%timeit: In cell mode, execute the code block several times for the best result
This allows us to quickly get the running time of a block of code with just a few keystrokes
Code words are not easy to nonsense two sentences: need python learning materials or technical questions to exchange “click”
4. View the current variables
As we write more and define more variables, it’s sometimes easy to forget which ones were named. It’s a pain to go back to the code, but with the Notebook, you can use %who_ls to see how many variables are currently defined
You can also specify variable types to view, such as which variables are strings
5. Delete multiple rows at a time
Sometimes indentation errors occur when copying someone else’s code into the Notebook
As shown in the image above, if we manually delete the white space in the red box, it would be boring and would require pressing the backspace key many times. In this case, we can hold down option(Alt in Win) and the cursor will become a cross. Now we can select the target area and delete it once
6. Direct access to documents
If you need to check the usage of some functions, you may need to search baidu or the official document and open many extra pages. In fact, you can use Shift + Tab to directly obtain the document of this method in the Notebook
Just like the image above, the usage of pd.merge is displayed directly, which is easy to see. Click on it to see a more detailed explanation
7. Load external files
The %load command can be used to load external files directly. For example, the %load test.py command can be used to open external files directly in the notebook, saving the time of page-to-copy and paste.
It is also possible to open the online documentation directly, such as the sample code in the official Matplotlib documentation
%load http://matplotlib.org/mpl_examples/pylab_examples/contour_demo.py
Copy the code
8. Run the Python script directly
Open external files directly, so can not run directly? The answer is yes, simply run the Python script in the notebook with %run xxx.py and print the result, for example running test.py in the current working directory
As a Python developer, I spent three days to compile a set of Python learning tutorials, from the most basic Python scripts to Web development, crawlers, data analysis, data visualization, machine learning, etc. These materials can be “clicked” by the friends who want them