top of page

[Python] Output pandas.DataFrame as json


Summary

Data analysis is performed using python. The analysis itself is performed using pandas, and the final results are stored in pandas.DataFrame format.

I want to output this result to a file in json format in order to use it in other applications.



How to do it

1 Convert pandas.DataFrame to dictionary

data = df.to_dict(orient='records')

2 Save to file

with open(file_path, 'w', encoding='utf-8') as file:
    json.dump(data, file, indent=4, ensure_ascii=False)

Specify the path of the file you want to save in file_path.

If it contains Japanese characters, set ensure_ascii = False to avoid garbled characters



Notes

pandas.DataFrame also has a method called to_json(), but don't use this.

to_json() is a method that converts to a json-like format string.

If you read the output of this, it will be read as one long string.

Recent Posts

See All

[Python] Conditionally fitting

Overview If you want to do fitting, you can do it with scipy.optimize.leastsq etc. in python. However, when doing fitting, there are many cases where you want to condition the fitting parameters. For

Comments


Let's do our best with our partner:​ ChatReminder

iphone6.5p2.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Let's do our best with our partner:​ ChatReminder

納品:iPhone6.5①.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Theme diary: Decide the theme and record for each genre

It is a diary application that allows you to post and record with themes and sub-themes for each genre.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png
bottom of page