Convert Pandas DataFrame to JSON format

前端 未结 7 1742
野性不改
野性不改 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)
    

提交回复
热议问题