Cara mengonversi pandas dataframe ke json


Seringkali Anda mungkin tertarik untuk mengonversi pandas DataFrame ke format JSON.

Untungnya, hal ini mudah dilakukan menggunakan fungsi to_json() , yang memungkinkan Anda mengonversi DataFrame menjadi string JSON dengan salah satu format berikut:

  • ‘split’: dict seperti {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]}
  • ‘catatan’: daftar seperti [{kolom -> nilai}, …, {kolom -> nilai}]
  • ‘indeks’: dict seperti {index -> {column -> value}}
  • ‘kolom’: dict seperti {kolom -> {index -> value}}
  • ‘nilai’: hanya serangkaian nilai
  • ‘tabel’: dict seperti {‘skema’: {skema}, ‘data’: {data}}

Tutorial ini menunjukkan cara mengonversi DataFrame ke masing-masing enam format menggunakan pandas DataFrame berikut:

 import pandas as pd

#createDataFrame
df = pd.DataFrame({'points': [25, 12, 15, 19],
                   'assists': [5, 7, 7, 12]})  

#view DataFrame
df

        assist points
0 25 5
1 12 7
2 15 7
3 19 12

Metode 1: “Bagi”

 df. to_json (orient=' split ')

{
   "columns": [
      "points",
      "assists"
   ],
   "index": [
      0,
      1,
      2,
      3
   ],
   "data": [
      [
         25,
         5
      ],
      [
         12,
         7
      ],
      [
         15,
         7
      ],
      [
         19,
         12
      ]
   ]
}

Metode 2: “Rekaman”

 df. to_json (orient=' records ')

[
   {
      "points": 25,
      “assists”: 5
   },
   {
      "points": 12,
      “assists”: 7
   },
   {
      "points": 15,
      “assists”: 7
   },
   {
      "points": 19,
      “assists”: 12
   }
]

Metode 3: “Indeks”

 df. to_json (orient=' index ') 

{
   "0": {
      "points": 25,
      “assists”: 5
   },
   "1": {
      "points": 12,
      “assists”: 7
   },
   "2": {
      "points": 15,
      “assists”: 7
   },
   "3": {
      "points": 19,
      “assists”: 12
   }
}

Metode 4: “Kolom”

 df. to_json (orient=' columns ') 

{
   "dots": {
      "0": 25,
      "1": 12,
      "2": 15,
      "3": 19
   },
   "assists": {
      "0": 5,
      "1": 7,
      "2": 7,
      "3": 12
   }
}

Metode 5: “Nilai”

 df. to_json (orient=' values ') 

[
   [
      25,
      5
   ],
   [
      12,
      7
   ],
   [
      15,
      7
   ],
   [
      19,
      12
   ]
]

Metode 6: “Meja”

 df. to_json (orient=' table ') 

{
   "plan": {
      "fields": [
         {
            "name": "index",
            "type": "integer"
         },
         {
            "name": "points",
            "type": "integer"
         },
         {
            "name": "assists",
            "type": "integer"
         }
      ],
      "primaryKey": [
         "index"
      ],
      "pandas_version": "0.20.0"
   },
   "data": [
      {
         "index": 0,
         "points": 25,
         “assists”: 5
      },
      {
         "index": 1,
         "points": 12,
         “assists”: 7
      },
      {
         "index": 2,
         "points": 15,
         “assists”: 7
      },
      {
         "index": 3,
         "points": 19,
         “assists”: 12
      }
   ]
}

Cara mengekspor file JSON

Anda dapat menggunakan sintaks berikut untuk mengekspor file JSON ke jalur file tertentu di komputer Anda:

 #create JSON file 
json_file = df. to_json (orient=' records ') 

#export JSON file
with open('my_data.json', 'w') as f:
    f.write(json_file)

Anda dapat menemukan dokumentasi lengkap fungsi pandas to_json() di sini .

Tambahkan komentar

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *