Let's say I have created a dataframe in one jupyter notebook:
In notebook_1:
df = pd.DataFrame(np.random.randn(100, 4), columns=list('ABCD'))
Now what is the best (easiest, fastest, most reliable, ...) way of moving df
into another notebook, so that I could work with it there?
In notebook_2:
df = ... #do something here to load the data from the df in notebook_1
#and now use the df further on
I came up with the following ways that (except possibly the last one) all work:
- export to a file and then import using one of the pandas IO tools - probably a good solution for very large dataframes, but otherwise seems unnecessarily complicated
- use pd.DataFrame.to_clipboard and pd.DataFrame.read_clipboard - looks good for small dataframes, but according to this answer it is not 100% reliable, plus it probably won't work for larger dataframes (?), plus if I rerun the notebook_2 after I replace the clipboard content with something else, it won't work anymore
- use pd.DataFrame.to_json and pd.DataFrame.read_json with
orient = 'table'
andpath_or_buf=None
and copy-paste the output - works well for my case (relatively small dataframe) - the upside is that I can see the imported data directly in the notebook_2 in plain text and so that notebook becomes self-contained once I first copy the data - copy-paste the whole cell containing the output of
df
??? - I couldn't test whether it copies also the data or just the code - the copy-paste doesn't work for me at all. But I doubt that.
Edit - Options mentioned in the comments or answers:
- the IPython %store magic as mentioned by Chris - really simple and nice, only downside is that the notebook_2 will not be self-contained
But I would like to know if there are other possible methods and what are the advantages, disadvantages, caveats, ...
I am interested more about insights, comparisons etc. than just one way how to do it (unless there is one, clearly the best, way that works perfectly in all scenarios and has no disadvantages).
Thanks.