我已阅读 JSON 文件,其中列出了名为内容的 COVID-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'},
如果我运行此代码
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'}
如何将其转换为 Pandas 数据框并使用字典键作为列名?
如果您的数据结构是字典列表,请执行以下操作:
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句