Dhruv Singhal
Dhruv Singhal

Reputation: 171

How to import dataframe from .py file to .ipynb file?

I am trying to import a dataframe from funcs.py file to data.ipynb file, both are in the same directory

funcs.py

df_course = pd.read_sql('SELECT COURSEID, COURSENAME FROM COURSE', con=conn)

here conn is the pyodbc connection , it is working fine as i am able to view dataframe in funcs.py

error

When i tried to import df_course in data.ipynb it is giving me error, i am doing like this

import funcs.df_course as df_course

but it is giving me error that cannot import df_course from funcs

Please help me in this problem

Upvotes: 2

Views: 432

Answers (4)

Anshuman Tekriwal
Anshuman Tekriwal

Reputation: 156

Instead Of import funcs.df_course as df_course;

Use This:- from funcs import df_course as df_course

For The Name That You Want, as df_course is not necessary, but if u want to import it as df or something, then you can use df_course..

Upvotes: 0

Volk
Volk

Reputation: 174

I recommend you to read this: https://jupyter.readthedocs.io/en/latest/contributing/docs-contributions/getting-started.html?highlight=ipynb#edit-the-documentation-source-file

honestly, It's difficult to understand what you want from Jupyter notebook. Did you use Jupyter? If yes, you have a file with extension .ipynb. but It's your code, it isn't your dataframe...

Upvotes: 0

Corralien
Corralien

Reputation: 120489

Simply use:

from funcs import df_course

because funcs is not a package, a directory that contains an __init__.py file.

Upvotes: 3

Sione Hoghen K
Sione Hoghen K

Reputation: 33

Try this:

import sys
function_path = r'C:\Users\hogsione\jupyter' # Path of funcs.py file
sys.path.insert(1, function_path)

# Now import df_course
from funcs import df_course

Upvotes: 1

Related Questions