andrew_helpme
andrew_helpme

Reputation: 33

How to import a pandas dataframe from a python file to another python file

I have two python files in the same folder: main.py and analysis.py.

In the analysis.py file I have a pandas dataframe called df1, inside a class called Ana(path, file)

I have imported the class to main.py successfully by writing from analysis import Ana, but if I try to do something with df1 it says df1 is not defined.

How do I define df1 in the main.py file? I am quite new to Python so any help will be very appreciated, thank you.

P.S. I forgot to add, I am trying to use the df1 from the Ana function in the analysis.py file in the function Upload in the main.py file

Upvotes: 0

Views: 7398

Answers (2)

Mikhail Vlasenko
Mikhail Vlasenko

Reputation: 140

Use Ana.df1 instead. It is a class's variable, so you first specify a class instance (or a class itself) and then df1

a = Ana()
a.df1

If you define df1 in the function Upload, you can add df1 to return values like this

def upload(args):
    #your code
    return your_return, df1

So now you can get df1 with return_value, df1 = Ana.upload()

Upvotes: 0

Ivan
Ivan

Reputation: 1455

Do a function returning Ana. If it's a member:

def ret_Ana():
    return self.Ana

Upvotes: 1

Related Questions