Convert Pandas DataFrame to JSON format

前端 未结 7 1642
野性不改
野性不改 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:10

    The output that you get after DF.to_json is a string. So, you can simply slice it according to your requirement and remove the commas from it too.

    out = df.to_json(orient='records')[1:-1].replace('},{', '} {')
    

    To write the output to a text file, you could do:

    with open('file_name.txt', 'w') as f:
        f.write(out)
    
    0 讨论(0)
  • 2020-11-30 02:13

    instead of using dataframe.to_json(orient = “records”) use dataframe.to_json(orient = “index”) my above code convert the dataframe into json format of dict like {index -> {column -> value}}

    0 讨论(0)
  • 2020-11-30 02:14

    In newer versions of pandas (0.20.0+, I believe), this can be done directly:

    df.to_json('temp.json', orient='records', lines=True)
    

    Direct compression is also possible:

    df.to_json('temp.json.gz', orient='records', lines=True, compression='gzip')
    
    0 讨论(0)
  • 2020-11-30 02:18

    To transform a dataFrame in a real json (not a string) I use:

        from io import StringIO
        import json
        import DataFrame
    
        buff=StringIO()
        #df is your DataFrame
        df.to_json(path_or_buf=buff,orient='records')
        dfJson=json.loads(buff)
    
    0 讨论(0)
  • 2020-11-30 02:20

    I think what the OP is looking for is:

    with open('temp.json', 'w') as f:
        f.write(df.to_json(orient='records', lines=True))
    

    This should do the trick.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题