If I have an existing pandas dataframe, is there a way to generate the python code, which when executed in another python script, will reproduce that dataframe.
e.g.
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]})
You could try to use the to_dict() method on DataFrame:
print "df = pd.DataFrame( %s )" % (str(df.to_dict()))
If your data contains NaN's, you'll have to replace them with float('nan'):
print "df = pd.DataFrame( %s )" % (str(df.to_dict()).replace(" nan"," float('nan')"))