Reputation: 14309
Every now and then I need to debug code and arrive to a point where I find a valid "oracle" or "fixture" for a new test case and then need to print the data frame to the console and turn it into a data frame initialization code. Is there a way to print a data frame into copyable code? I know I could just dump it in a file and then use the file for input to a test case, and this should be the case for very long data frames but for small ones it would be nice to be able to copy paste them into a test suite ...
Upvotes: 6
Views: 1372
Reputation: 61910
If what you seek is representation of the DataFrame that could be use as valid Python code as suggest you use to_dict, as follows:
import pandas as pd
df = pd.DataFrame(data=list(enumerate(['Dan Cat', 'Dave Dog', 'Diego Cangrejo'], 1)), columns=['id', 'name'])
print(df.to_dict())
Output
{'id': {0: 1, 1: 2, 2: 3}, 'name': {0: 'Dan Cat', 1: 'Dave Dog', 2: 'Diego Cangrejo'}}
The output of the above state is valid Python dictionary literal, so you can copy it directly in the code:
import pandas as pd
data = {'id': {0: 1, 1: 2, 2: 3}, 'name': {0: 'Dan Cat', 1: 'Dave Dog', 2: 'Diego Cangrejo'}}
print(pd.DataFrame.from_dict(data))
Output
id name
0 1 Dan Cat
1 2 Dave Dog
2 3 Diego Cangrejo
Upvotes: 6