0

I create a pandas dataframe 'df' in a notebook A, I need use the data frame 'df' in a notebook B. I do not know what can i do. How call df in the notebook B to make some computations and create anothers dataframes

I tried this:

from notbookA import df

but does not work. I want manipulate df in the notebook B. i Think the problem is beacause i used notebooks instead python files but i need use notebooks.

I want something like :

`from notbookA import df`

#example
df2 = df.apply(lamnda x: someFunction(x))
  • 1
    perhaps `pandas.DataFrame.to_pickle()` might help here (assuming that the dataframe in question is not static). – JonSG Feb 23 '23 at 00:32

2 Answers2

2

As @JonSG mentioned, you can use pickle.

In the first notebook you need to export your dataframe as a pickle:

df.to_pickle('df.pkl')

And in the second notebook you can import the pickle file as dataframe:

df = pd.read_pickle('df.pkl')

This will work assuming both notebooks are in the same folder, and will create the df.pkl file in that folder.

You could also use to_csv/read_csv or to_excel/read_excel depending on your needs and the type of data.

Mattravel
  • 1,358
  • 1
  • 15
1

IIUC, you're looking for storemagic from :

Stores variables, aliases and macros in IPython’s database.

In A.ipynb,

df = pd.DataFrame(...)

%store df # <- add this line after the instantiation of df

In B.ipynb,

%store -r df # to manually restore df from A.ipynb

df2 = df.apply(lambda x: someFunction(x))
Timeless
  • 22,580
  • 4
  • 12
  • 30