Working With JSON FIles
You can save the data from your DataFrame to a JSON file with
.to_json(). This code produces the file
data-columns.json. You can see its contents onscreen now. It has one large dictionary with the column labels as keys and the corresponding inner dictionaries as values.
orient parameter defaults to
'columns', but here it’s been set to
'index'. You should get a new file
data-index.json, whose contents you can see onscreen now. You can see that it also has one large dictionary, but this time the row labels are the keys and the inner dictionaries are the values. There are a few more options for
The resulting file is
data-split.json, whose contents you can see onscreen now.
data-split.json contains one dictionary that holds the following list: the names of the columns, the labels of the rows, the inner lists that hold data values.
If you don’t provide the value for the optional parameter
path_or_buf that defines the file path, then
.to_json() will return a JSON string instead of writing the results to a file, exactly as you saw earlier on with
There are other optional parameters you can use, for instance, you can set
index=False to forgo saving row labels. You can manipulate precision with
double_precision, and dates with
These last two parameters are particularly important when you have time series amongst your data. In this example,
.to_datetime() has been used to convert the values in the last column to
You can see the results of this onscreen now. In this file, the dates are represented as large integers. That’s because the default value of the optional parameter
convert_dates has a similar purpose as
parse_dates when you use it to read CSV files. The optional parameter
orient is very important because it specifies how pandas understands the structure of the file.
Become a Member to join the conversation.