Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将包含多个字典的JSON文件转换为数据帧_Python_Json_Pandas - Fatal编程技术网

Python 将包含多个字典的JSON文件转换为数据帧

Python 将包含多个字典的JSON文件转换为数据帧,python,json,pandas,Python,Json,Pandas,我已经阅读了JSON文件,其中列出了名为内容的新冠病毒-19患者。我想将文件转换为pandas数据帧,以便使用Python进行分析。不幸的是,该文件位于多个词典中 内容如下所示 [{'id': 'from_manual_250627', 'kode_kab': '3204', 'nama_kab': 'Kabupaten Bandung', 'kode_kec': '3204130', 'nama_kec': 'Ciparay', 'kode_kel': '320413000

我已经阅读了JSON文件,其中列出了名为内容的新冠病毒-19患者。我想将文件转换为pandas数据帧,以便使用Python进行分析。不幸的是,该文件位于多个词典中

内容如下所示

[{'id': 'from_manual_250627',
  'kode_kab': '3204',
  'nama_kab': 'Kabupaten Bandung',
  'kode_kec': '3204130',
  'nama_kec': 'Ciparay',
  'kode_kel': '3204130005',
  'nama_kel': 'Manggungharja',
  'status': 'CONFIRMATION',
  'stage': 'Selesai',
  'umur': 29.0,
  'gender': 'Laki-laki',
  'longitude': 107.698,
  'latitude': -7.045,
  'tanggal_konfirmasi': '2021-03-30',
  'tanggal_update': '2021-05-10',
  'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan',
  'current_location_district_code': None,
  'current_location_subdistrict_code': None,
  'current_location_village_code': None,
  'current_location_address': None,
  'report_source': None,
  'tanggal_update_nasional': '2021-04-01'},
 {'id': 'from_manual_170876',
  'kode_kab': '3204',
  'nama_kab': 'Kabupaten Bandung',
  'kode_kec': '3204',
  'nama_kec': 'Lembursitu',
  'kode_kel': '3204',
  'nama_kel': 'Lembursitu',
  'status': 'CONFIRMATION',
  'stage': 'Selesai',
  'umur': 45.0,
  'gender': 'Perempuan',
  'longitude': 107.611,
  'latitude': -7.1,
  'tanggal_konfirmasi': '2021-02-09',
  'tanggal_update': '2021-03-01',
  'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan',
  'current_location_district_code': None,
  'current_location_subdistrict_code': None,
  'current_location_village_code': None,
  'current_location_address': None,
  'report_source': None,
  'tanggal_update_nasional': '2021-02-11'},
 {'id': 'from_manual_171165',
  'kode_kab': '3204',
  'nama_kab': 'Kabupaten Bandung',
  'kode_kec': '3204',
  'nama_kec': 'Purwakarta',
  'kode_kel': '3204',
  'nama_kel': 'Munjuljaya',
  'status': 'CONFIRMATION',
  'stage': 'Selesai',
  'umur': 20.0,
  'gender': 'Perempuan',
  'longitude': 107.611,
  'latitude': -7.1,
  'tanggal_konfirmasi': '2021-02-08',
  'tanggal_update': '2021-03-01',
  'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan',
  'current_location_district_code': None,
  'current_location_subdistrict_code': None,
  'current_location_village_code': None,
  'current_location_address': None,
  'report_source': None,
  'tanggal_update_nasional': '2021-02-11'},
{'id': 'from_manual_250627', 'kode_kab': '3204', 'nama_kab': 'Kabupaten Bandung', 'kode_kec': '3204130', 'nama_kec': 'Ciparay', 'kode_kel': '3204130005', 'nama_kel': 'Manggungharja', 'status': 'CONFIRMATION', 'stage': 'Selesai', 'umur': 29.0, 'gender': 'Laki-laki', 'longitude': 107.698, 'latitude': -7.045, 'tanggal_konfirmasi': '2021-03-30', 'tanggal_update': '2021-05-10', 'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan', 'current_location_district_code': None, 'current_location_subdistrict_code': None, 'current_location_village_code': None, 'current_location_address': None, 'report_source': None, 'tanggal_update_nasional': '2021-04-01'}
{'id': 'from_manual_170876', 'kode_kab': '3204', 'nama_kab': 'Kabupaten Bandung', 'kode_kec': '3204', 'nama_kec': 'Lembursitu', 'kode_kel': '3204', 'nama_kel': 'Lembursitu', 'status': 'CONFIRMATION', 'stage': 'Selesai', 'umur': 45.0, 'gender': 'Perempuan', 'longitude': 107.611, 'latitude': -7.1, 'tanggal_konfirmasi': '2021-02-09', 'tanggal_update': '2021-03-01', 'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan', 'current_location_district_code': None, 'current_location_subdistrict_code': None, 'current_location_village_code': None, 'current_location_address': None, 'report_source': None, 'tanggal_update_nasional': '2021-02-11'}
{'id': 'from_manual_171165', 'kode_kab': '3204', 'nama_kab': 'Kabupaten Bandung', 'kode_kec': '3204', 'nama_kec': 'Purwakarta', 'kode_kel': '3204', 'nama_kel': 'Munjuljaya', 'status': 'CONFIRMATION', 'stage': 'Selesai', 'umur': 20.0, 'gender': 'Perempuan', 'longitude': 107.611, 'latitude': -7.1, 'tanggal_konfirmasi': '2021-02-08', 'tanggal_update': '2021-03-01', 'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan', 'current_location_district_code': None, 'current_location_subdistrict_code': None, 'current_location_village_code': None, 'current_location_address': None, 'report_source': None, 'tanggal_update_nasional': '2021-02-11'}
如果我运行这个代码

    for dict in content:
    print(dict)
输出如下

[{'id': 'from_manual_250627',
  'kode_kab': '3204',
  'nama_kab': 'Kabupaten Bandung',
  'kode_kec': '3204130',
  'nama_kec': 'Ciparay',
  'kode_kel': '3204130005',
  'nama_kel': 'Manggungharja',
  'status': 'CONFIRMATION',
  'stage': 'Selesai',
  'umur': 29.0,
  'gender': 'Laki-laki',
  'longitude': 107.698,
  'latitude': -7.045,
  'tanggal_konfirmasi': '2021-03-30',
  'tanggal_update': '2021-05-10',
  'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan',
  'current_location_district_code': None,
  'current_location_subdistrict_code': None,
  'current_location_village_code': None,
  'current_location_address': None,
  'report_source': None,
  'tanggal_update_nasional': '2021-04-01'},
 {'id': 'from_manual_170876',
  'kode_kab': '3204',
  'nama_kab': 'Kabupaten Bandung',
  'kode_kec': '3204',
  'nama_kec': 'Lembursitu',
  'kode_kel': '3204',
  'nama_kel': 'Lembursitu',
  'status': 'CONFIRMATION',
  'stage': 'Selesai',
  'umur': 45.0,
  'gender': 'Perempuan',
  'longitude': 107.611,
  'latitude': -7.1,
  'tanggal_konfirmasi': '2021-02-09',
  'tanggal_update': '2021-03-01',
  'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan',
  'current_location_district_code': None,
  'current_location_subdistrict_code': None,
  'current_location_village_code': None,
  'current_location_address': None,
  'report_source': None,
  'tanggal_update_nasional': '2021-02-11'},
 {'id': 'from_manual_171165',
  'kode_kab': '3204',
  'nama_kab': 'Kabupaten Bandung',
  'kode_kec': '3204',
  'nama_kec': 'Purwakarta',
  'kode_kel': '3204',
  'nama_kel': 'Munjuljaya',
  'status': 'CONFIRMATION',
  'stage': 'Selesai',
  'umur': 20.0,
  'gender': 'Perempuan',
  'longitude': 107.611,
  'latitude': -7.1,
  'tanggal_konfirmasi': '2021-02-08',
  'tanggal_update': '2021-03-01',
  'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan',
  'current_location_district_code': None,
  'current_location_subdistrict_code': None,
  'current_location_village_code': None,
  'current_location_address': None,
  'report_source': None,
  'tanggal_update_nasional': '2021-02-11'},
{'id': 'from_manual_250627', 'kode_kab': '3204', 'nama_kab': 'Kabupaten Bandung', 'kode_kec': '3204130', 'nama_kec': 'Ciparay', 'kode_kel': '3204130005', 'nama_kel': 'Manggungharja', 'status': 'CONFIRMATION', 'stage': 'Selesai', 'umur': 29.0, 'gender': 'Laki-laki', 'longitude': 107.698, 'latitude': -7.045, 'tanggal_konfirmasi': '2021-03-30', 'tanggal_update': '2021-05-10', 'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan', 'current_location_district_code': None, 'current_location_subdistrict_code': None, 'current_location_village_code': None, 'current_location_address': None, 'report_source': None, 'tanggal_update_nasional': '2021-04-01'}
{'id': 'from_manual_170876', 'kode_kab': '3204', 'nama_kab': 'Kabupaten Bandung', 'kode_kec': '3204', 'nama_kec': 'Lembursitu', 'kode_kel': '3204', 'nama_kel': 'Lembursitu', 'status': 'CONFIRMATION', 'stage': 'Selesai', 'umur': 45.0, 'gender': 'Perempuan', 'longitude': 107.611, 'latitude': -7.1, 'tanggal_konfirmasi': '2021-02-09', 'tanggal_update': '2021-03-01', 'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan', 'current_location_district_code': None, 'current_location_subdistrict_code': None, 'current_location_village_code': None, 'current_location_address': None, 'report_source': None, 'tanggal_update_nasional': '2021-02-11'}
{'id': 'from_manual_171165', 'kode_kab': '3204', 'nama_kab': 'Kabupaten Bandung', 'kode_kec': '3204', 'nama_kec': 'Purwakarta', 'kode_kel': '3204', 'nama_kel': 'Munjuljaya', 'status': 'CONFIRMATION', 'stage': 'Selesai', 'umur': 20.0, 'gender': 'Perempuan', 'longitude': 107.611, 'latitude': -7.1, 'tanggal_konfirmasi': '2021-02-08', 'tanggal_update': '2021-03-01', 'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan', 'current_location_district_code': None, 'current_location_subdistrict_code': None, 'current_location_village_code': None, 'current_location_address': None, 'report_source': None, 'tanggal_update_nasional': '2021-02-11'}

如何将其转换为pandas数据帧并使用dictionary键作为列的名称?

如果您的数据结构是一个字典列表,请执行以下操作:

import pandas as pd

list_of_dicts = [{'id': 'from_manual_250627', 'kode_kab': '3204', 'nama_kab': 'Kabupaten Bandung', 'kode_kec': '3204130', 'nama_kec': 'Ciparay', 'kode_kel': '3204130005', 'nama_kel': 'Manggungharja', 'status': 'CONFIRMATION', 'stage': 'Selesai', 'umur': 29.0, 'gender': 'Laki-laki', 'longitude': 107.698, 'latitude': -7.045, 'tanggal_konfirmasi': '2021-03-30', 'tanggal_update': '2021-05-10', 'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan', 'current_location_district_code': None, 'current_location_subdistrict_code': None, 'current_location_village_code': None, 'current_location_address': None, 'report_source': None, 'tanggal_update_nasional': '2021-04-01'}, {'id': 'from_manual_170876', 'kode_kab': '3204', 'nama_kab': 'Kabupaten Bandung', 'kode_kec': '3204', 'nama_kec': 'Lembursitu', 'kode_kel': '3204', 'nama_kel': 'Lembursitu', 'status': 'CONFIRMATION', 'stage': 'Selesai', 'umur': 45.0, 'gender': 'Perempuan', 'longitude': 107.611, 'latitude': -7.1, 'tanggal_konfirmasi': '2021-02-09', 'tanggal_update': '2021-03-01', 'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan', 'current_location_district_code': None, 'current_location_subdistrict_code': None, 'current_location_village_code': None, 'current_location_address': None, 'report_source': None, 'tanggal_update_nasional': '2021-02-11'}, {'id': 'from_manual_171165', 'kode_kab': '3204', 'nama_kab': 'Kabupaten Bandung', 'kode_kec': '3204', 'nama_kec': 'Purwakarta', 'kode_kel': '3204', 'nama_kel': 'Munjuljaya', 'status': 'CONFIRMATION', 'stage': 'Selesai', 'umur': 20.0, 'gender': 'Perempuan', 'longitude': 107.611, 'latitude': -7.1, 'tanggal_konfirmasi': '2021-02-08', 'tanggal_update': '2021-03-01', 'current_location_type': 'Rumah Sakit Umum Daerah Al Ihsan', 'current_location_district_code': None, 'current_location_subdistrict_code': None, 'current_location_village_code': None, 'current_location_address': None, 'report_source': None, 'tanggal_update_nasional': '2021-02-11'}]

df = pd.DataFrame(list_of_dicts)