9951 explained code solutions for 126 technologies


python-pandasSave dataframe to JSON


import pandas as pd

df = pd.DataFrame({
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Price': [204, 304, 404, 405, 305],
  'Color': ['red', 'red', 'gray', 'black', 'red']
})

json = df.to_json()ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

to_json()

saves dataframe to JSON string


Usage example

import pandas as pd

df = pd.DataFrame({
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Price': [204, 304, 404, 405, 305],
  'Color': ['red', 'red', 'gray', 'black', 'red']
})

json = df.to_json()
print(json)
output
{"Phone":{"0":"ip5","1":"ip6","2":"ip8","3":"sms","4":"xi"},"Price":{"0":204,"1":304,"2":404,"3":405,"4":305},"Color":{"0":"red","1":"red","2":"gray","3":"black","4":"red"}}