Mol
Mol

Reputation: 43

Importing a dataframe to another script

I have a scripts (script 1) in python that produce a dataframe like this one:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([[1, 2], [4, 5]]), index=('27-04-2020','28-04-2020'), columns=('Prediction', 'Certainty'))

I want to import the dataframe into (script A). However, I can not seem to find a way for importing dataframes. Can someone help me with a solution for this?

Upvotes: 1

Views: 4345

Answers (2)

Ken Adams
Ken Adams

Reputation: 11

@user2077935 has the right start.

You just need to add a return statement.

So script_1.py

def get_dataframe():
    df = pd.DataFrame(np.array([[1, 2], [4, 5]]), index=('27-04-2020','28-04-2020'), columns=('Prediction', 'Certainty'))
    return df

The rest stays the same

Upvotes: 1

user2077935
user2077935

Reputation: 388

You can simply do this in script_a.py as

from script_1 import df

But a cleaner way to do that would be: In script_1.py

import pandas as pd
import numpy as np

def get_dataframe():
    df = pd.DataFrame(np.array([[1, 2], [4, 5]]), index=('27-04-2020','28-04-2020'), columns=('Prediction', 'Certainty'))

In script_a.py

from script_1 import get_dataframe
df = get_dataframe()

Upvotes: 2

Related Questions