This recipe demonstrates the conversion of DataFrame objects into other formats, such as .csv files, json objects, and pickle objects. Conversion into a .csv file makes it easier to further work on the data using a spreadsheet application. The json format is useful for transmitting DataFrame objects over web APIs. The pickle format is useful for transmitting DataFrame objects created in one Python session to another Python session over sockets without having to recreate them.
Getting ready
Make sure the object df is available in your Python namespace. Refer to Creating a pandas.DataFrame object recipe of this chapter to set up this object.
How to do it…
Execute the following steps for this recipe:
- Convert and save df as a CSV file:
>>> df.to_csv('dataframe.csv', index=False)
- Convert df to a JSON string:
>>> df.to_json()
We get the following output:
'{
"timestamp":{
"0":...