- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How to get a variable from one Notebook into another
I'm running a notebook, which output needs to be stored and used in a new notebook. How can this be done?
Let's say in Notebook_1 the code is
import pandas as pd
# Create an example pandas DataFrame
data = {
"ID": [1, 2, 3, 4, 5],
"Name": ["Alice", "Bob", "Charlie", "David", "Eve"],
"Age": [25, 30, 35, 40, 45],
"Salary": [50000, 60000, 70000, 80000, 90000],
"Department": ["HR", "IT", "Finance", "Marketing", "Sales"]
}
df = pd.DataFrame(data)
I want to reference df in a new notebook so I have no need to rewrite the whole code in the new notebook.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello @CrhIT
The `%run` magic command allows you to execute another notebook within the same workspace and access its variables directly.
Notebook1
import pandas as pd
data = {
"ID": [1, 2, 3, 4, 5],
"Name": ["Alice", "Bob", "Charlie", "David", "Eve"],
"Age": [25, 30, 35, 40, 45],
"Salary": [50000, 60000, 70000, 80000, 90000],
"Department": ["HR", "IT", "Finance", "Marketing", "Sales"]
}
df = pd.DataFrame(data)
In Notebook_2, use `%run` to call Notebook_1:
%run Notebook_1
print(df.head())
This method works only if both notebooks are in the same workspace.
• Variables defined in Notebook_1 will be available in Notebook_2
You can use `mssparkutils.notebook.run()`
Hope this is helpful.
please accept the answer and give kudos if this helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@CrhIT if this is helpful , please accept the answer as it will help community to find the right information quickly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello @CrhIT
The `%run` magic command allows you to execute another notebook within the same workspace and access its variables directly.
Notebook1
import pandas as pd
data = {
"ID": [1, 2, 3, 4, 5],
"Name": ["Alice", "Bob", "Charlie", "David", "Eve"],
"Age": [25, 30, 35, 40, 45],
"Salary": [50000, 60000, 70000, 80000, 90000],
"Department": ["HR", "IT", "Finance", "Marketing", "Sales"]
}
df = pd.DataFrame(data)
In Notebook_2, use `%run` to call Notebook_1:
%run Notebook_1
print(df.head())
This method works only if both notebooks are in the same workspace.
• Variables defined in Notebook_1 will be available in Notebook_2
You can use `mssparkutils.notebook.run()`
Hope this is helpful.
please accept the answer and give kudos if this helps

Helpful resources
Join us at the Microsoft Fabric Community Conference
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Fabric Monthly Update - February 2025
Check out the February 2025 Fabric update to learn about new features.

Subject | Author | Posted | |
---|---|---|---|
05-10-2024 12:53 AM | |||
10-31-2023 04:43 AM | |||
10-24-2024 08:46 AM | |||
Anonymous
| 06-14-2024 12:22 AM | ||
09-13-2023 07:50 PM |
User | Count |
---|---|
8 | |
1 | |
1 | |
1 | |
1 |
User | Count |
---|---|
15 | |
5 | |
5 | |
3 |