Pandas dataframe to code

If I have an existing pandas framework, is there a way to generate python code that, when executed in another python script, will play that data frame.

eg.

In[1]: df Out[1]: income user 0 40000 Bob 1 50000 Jane 2 42000 Alice In[2]: someFunToWriteDfCode(df) Out[2]: df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice'], ...: 'income': [40000, 50000, 42000]}) 
+6
source share
2 answers

You can try using the to_dict () method in the DataFrame:

 print "df = pd.DataFrame( %s )" % (str(df.to_dict())) 

If your data contains NaN, you will have to replace it with float ('nan'):

 print "df = pd.DataFrame( %s )" % (str(df.to_dict()).replace(" nan"," float('nan')")) 
+2
source

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) # save df in a file named "dataframe" 

To read the data file in another file:

 import pickle with open('dataframe', 'rb') as pfile: df2 = pickle.load(pfile) # read the dataframe stored in file "dataframe" print(df2) 

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 # store the dataframe in file "dataframe" 

To read the data file in another file:

 import shelve with shelve.open('dataframe2') as shelf: print(shelf['df']) # read the dataframe 

Output:

  income user 0 40000 Bob 1 50000 Jane 2 42000 Alice 
-one
source

Source: https://habr.com/ru/post/1014263/


All Articles