简体   繁体   中英

Execution order of jupyter notebook cells

I am doing a new data analytic project with jupyter notebook, and I am confused about the order of notebook cells.

I firstly import pandas and read the csv file as data, so my first cell looks like:

In [1]:

import pandas as pd
data = pd.read_csv('thanksgiving.csv', encoding='Latin-1')
print(data.head(5))

The I wanna print out the column names of the dataframe:

In [2]:
data.columns

Then I realize that in the first cell, I should use data.head(5) instead of print(data.head(5)), because the print function doesn't print the dataframe in proper format.

So I go back to the 1st cell, modify and execute it again. Then it changes from: In [1] to In [3]. The 2 cells now looks like:

In [3]: ......
In [2]: ......

More specifically, the order of cells messed up. I am afraid this will confuse the readers of my project. Is there a well accepted regulation on this issue? Or I just have to pay extra attention to avoid re-run the cells in the beginning?

Jupyter notebooks work like this only.

If you have modified any cell in the notebook, then you have to re-run its succeeding cells also. And that would make the cells in ascending order again.

In your example, when the cells look like this, cell with number 2 should be executed again due to some changes in some preceding cell.

In [3]: ......
In [2]: ......

After you run cell 2, then the notebook will look like this.

In [3]: ......
In [4]: ......

Please always re-run your notebooks from top-to-bottom before sharing. Make this a rule to live by. Because even if you re-run a few cells in order, there still may be unknown changes that occurred.

If we have

In [1]: ......
In [47]: ......
In [46]: ......
In [4]: ......

It doesn't matter if I re-run 46 and 47 to be "in order". There's still 42 operations (unknown cell executions) between execution 4 and 46! It's therefore impossible for others to understand what happened because it was possible to change the code of that cell. Therefore, you will save yourself some headache if you do re-run before sharing.

In [1]: ......
In [2]: ......
In [3]: ......
In [4]: ......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM