You can save the data you have first and then load it into another python script when necessary. You can do this with two packages: pickle
and shelve
.
Do this with pickle
:
import pandas as pd import pickle df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice'], 'income': [40000, 50000, 42000]}) with open('dataframe', 'wb') as pfile: pickle.dump(df, pfile)
To read the data file in another file:
import pickle with open('dataframe', 'rb') as pfile: df2 = pickle.load(pfile)
Output:
income user 0 40000 Bob 1 50000 Jane 2 42000 Alice
Do this with shelve
:
import pandas as pd import shelve df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice'], 'income': [40000, 50000, 42000]}) with shelve.open('dataframe2') as shelf: shelf['df'] = df
To read the data file in another file:
import shelve with shelve.open('dataframe2') as shelf: print(shelf['df'])
Output:
income user 0 40000 Bob 1 50000 Jane 2 42000 Alice
source share