Convert Pandas DataFrame to JSON format

前端 未结 7 1723
野性不改
野性不改 2020-11-30 01:45

I have a Pandas DataFrame with two columns – one with the filename and one with the hour in which it was generated:

 File       Hour
  F1               


        
7条回答
  •  既然无缘
    2020-11-30 02:23

    Here is small utility class that converts JSON to DataFrame and back: Hope you find this helpful.

    # -*- coding: utf-8 -*-
    from pandas.io.json import json_normalize
    
    class DFConverter:
    
        #Converts the input JSON to a DataFrame
        def convertToDF(self,dfJSON):
            return(json_normalize(dfJSON))
    
        #Converts the input DataFrame to JSON 
        def convertToJSON(self, df):
            resultJSON = df.to_json(orient='records')
            return(resultJSON)
    

提交回复
热议问题