1 | initial version |
When converting a DataFrame between JSON and CSV formats, the multi-index can be maintained by setting the appropriate parameters:
.to_json()
method with the orient='records'
parameter. This will export the DataFrame as a list of dictionaries, where each dictionary represents a row and its keys correspond to the column names and the index levels with a hierarchical key name.pandas.json_normalize()
function with the appropriate record_path
, meta
, and record_prefix
parameters to flatten the hierarchical JSON structure into a DataFrame with a multi-index..to_csv()
method with the header=True
and index=True
parameters. The index levels will be separated by a comma in the header row and in the corresponding rows of the DataFrame.index_col
parameter with a list of integers or strings that correspond to the index levels. For example, index_col=[0,1]
will create a multi-index with the first and second columns as the index levels.