0

I have two scripts, let's say "reading" and "calculation". Script "reading" has inside of it several functions, and reads data from csv files and creates pandas dataframes. Let's say it defines variable "dataframe_1". Script "calculation" makes some calculation with dataframes, which were defined in script "reading".

I want to run script "calculation" after I ran script "reading", and use its results somehow. How can I access that variable (dataframe_1) inside script "calculation", after it was defined in script "reading"?

I assume during execution of script "reading", dataframe_1 is stored in operating memory, how can I access it?

If I do it like that, it reads dataframe_1 again, instead of just accessing the result of reading, which are stored in memory?

from reading import dataframe_1
Sirakan
  • 3
  • 2
  • Please provide enough code so others can better understand or reproduce the problem. – Community Oct 25 '22 at 10:28
  • Welcome to SO! Can you share with us what you are tried so far? – medium-dimensional Oct 25 '22 at 10:34
  • 1
    @medium-dimensional thanks! I've tried to import variables from another script, but it just runs the part of script, which defines the variable, in other word - just reads the file again. I want to access the variable from memory, to not read it again. – Sirakan Oct 25 '22 at 11:46

2 Answers2

0

First write a function or a method that returns a dataframe in reading.py e.g.

def foo(arguments):
   # create dataframe
   # return dataframe

Then, import foo in calculation.py to generate dataframe:

from reading import foo
dataframe = foo(arguments)

# do calculations on dataframe

OR

In reading.py:

def foo(arguments):
   # create dataframe
   # return dataframe

# Store a data frame
data = foo(arguments) 

and in calculation.py:

import reading

data = reading.data

Variable names that we create are references to the objects stored in memory in Python. So, calculation.py must know where the dataset created by reading.py is stored in memory. Here, the above method creates only one instance of dataframe.

You might want to check Other languages have "variables", Python has "names" and how importing works in Python.

medium-dimensional
  • 1,974
  • 10
  • 19
  • Thanks for the answer! I've got such function, and it reads csv file to dataframe in reading.py. What I want to do - is to pass this dataframe to other script, without need to read it from csv again, because dataframe is pretty large (~6 mln rows). – Sirakan Oct 25 '22 at 11:20
  • @Sirakan The method suggested in the answer actually reads the csv file only once - when you call `foo` in calculation.py :) – medium-dimensional Oct 25 '22 at 11:28
  • Thanks for the links as well, I indeed didn't know that variables (names) in python work not like I expected. – Sirakan Oct 25 '22 at 11:47
  • I mean, if I will have another script "calculation_2", which will do some other calculations, I will need to use that function again. And actually there indeed will be many scripts, which will use data, which I want to read in "reading". The whole idea of having "reading" is to get rid of need to read these csv files again and again. – Sirakan Oct 25 '22 at 12:39
0

There are multiple ways to import variable. One way is this.

# settings.py

def init():
    global myList
    myList = []

Next, your file can import variables:

# file1.py

import settings

def stuff():
    settings.myList.append('hey')

Note that file1 does not call init()— that task belongs to main.py:

# main.py

import settings
import subfile

settings.init()          # Call only once
subfile.stuff()         # Do stuff with global var
print settings.myList[0] # Check the result
Aniket Kumar
  • 165
  • 2
  • 10
  • Yeah, but if I do it like that, it kind of "runs" the filename and gets the variable from there. And I don't want to run that script again, I want that variable to be stored somewhere in memory, and get quick access to it. – Sirakan Oct 25 '22 at 11:56
  • This is cleanest approach, create a new file and create all the variable there using keyword global. Updated my answer – Aniket Kumar Oct 27 '22 at 07:08