【问题标题】:Loading JSON data into a dataframe将 JSON 数据加载到数据框中
【发布时间】:2021-11-17 06:56:59
【问题描述】:

我是使用 pandas 和 python 的新手,我正在尝试将我拥有的 JSON 文件加载到 jupyter 笔记本中。我在将数据放入 Dataframe 时遇到了困难。

    os.chdir('M:\JT\Voyages Data')
    with open('M:\JT\Voyages Data\Data.json') as data:
        data = json.load(data)
        df = pd.DataFrame(data)

我无法打印出来,因为我不断收到错误提示

arrays must all be the same length

编辑***

很抱歉,这里没有尽快包含 JSON 文件中一些数据的副本,

{'columns': ['total', 'results', 'site'],
 'index': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 'data': [[157773,
   [{'load_zone': 'East Mediterranean',
     'load_country': 'Turkey',
     'load_port': 'Bandirma',
     'load_berth': None,
     'load_anchorage': None,
     'load_draft': 7.9,
     'load_duration': None,
     'load_full_duration': None,
     'load_wait_duration': None,
     'load_full_wait_duration': None,
     'load_start_date': '2013-07-16 03:43:19',
     'load_end_date': '2013-07-17 03:43:19',
     'discharge_zone': 'North West Africa',
     'discharge_country': 'Morocco',
     'discharge_port': 'Safi',
     'discharge_berth': 'Quai De Commerce',
     'discharge_anchorage': None,
     'discharge_draft': 4.5,
     'discharge_duration': 1734,
     'discharge_full_duration': 2406,
     'discharge_wait_duration': 0,
     'discharge_full_wait_duration': 0,
     'discharge_start_date': '2013-08-15 08:28:59',
     'discharge_end_date': '2013-08-16 13:23:00',
     'commodity': 'Steam Coal',
     'commodity_group': 'Coal',
     'charterer': None,
     'vsl_imo': 9045742,
     'vsl_dwt': 6584,
     'vsl_name': 'OSTRA',
     'vsl_type': 'Multi-Purpose',
     'vsl_max_speed': 0,
     'vsl_max_draft': 7.6,
     'has_part_voy': 1,
     'voy_draft_diff': 3.4,
     'voy_load_draft_ratio': 103.947,
     'voy_intake_mt': 5926,
     'voy_avg_speed': 11.26,
     'voy_speed_ratio': None,
     'voy_top_speed': 13.38,
     'voy_duration': 45220,
     'voy_sea_duration': 42813,
     'bunk_duration': 0,
     'bunk_wait_duration': 0,
     'blackout_duration': 18645,
     'shipyard_duration': 0,
     'previous_zone': 'Black Sea',
     'previous_port': 'Efesan Port',
     'previous_berth': None,
     'previous_operation': 'disch',
     'next_zone': 'North West Africa',
     'next_port': 'Jorf Lasfar',
     'next_berth': 'Berth 2 Phosphate / Fertilizers',
     'next_operation': 'load'},
    {'load_zone': 'North Coast South America',
     'load_country': 'Colombia',
     'load_port': 'Puerto Nuevo - Colombia',
     'load_berth': 'Puerto Nuevo Coal Pier',
     'load_anchorage': None,
     'load_draft': 18.1,
     'load_duration': 4685,
     'load_full_duration': 4685,
     'load_wait_duration': 421,
     'load_full_wait_duration': 421,
     'load_start_date': '2013-07-25 15:17:47',
     'load_end_date': '2013-07-28 21:22:56',
     'discharge_zone': 'Antwerp Rotterdam Amsterdam Ghent',
     'discharge_country': 'Netherlands',
     'discharge_port': 'Rotterdam',
     'discharge_berth': 'EMO - Europees Massagoed- Overslagbedrijf ',
     'discharge_anchorage': None,
     'discharge_draft': 9.9,
     'discharge_duration': 1112,
     'discharge_full_duration': 1112,
     'discharge_wait_duration': 0,
     'discharge_full_wait_duration': 0,
     'discharge_start_date': '2013-08-15 08:45:24',
     'discharge_end_date': '2013-08-16 03:17:29',
     'commodity': 'Steam Coal',
     'commodity_group': 'Coal',
     'charterer': None,
     'vsl_imo': 9581760,
     'vsl_dwt': 181415,
     'vsl_name': 'Sea Triumph',
     'vsl_type': 'Bulk Carrier',
     'vsl_max_speed': 0,
     'vsl_max_draft': 18.3,
     'has_part_voy': 0,
     'voy_draft_diff': 8.2,
     'voy_load_draft_ratio': 98.9071,
     'voy_intake_mt': 172851,
     'voy_avg_speed': 12.19,
     'voy_speed_ratio': None,
     'voy_top_speed': 13.65,
     'voy_duration': 30960,
     'voy_sea_duration': 25162,
     'bunk_duration': 0,
     'bunk_wait_duration': 0,
     'blackout_duration': 0,
     'shipyard_duration': 0,
     'previous_zone': 'Antwerp Rotterdam Amsterdam Ghent',
     'previous_port': 'Rotterdam',
     'previous_berth': 'EMO - Europees Massagoed- Overslagbedrijf ',
     'previous_operation': 'disch',
     'next_zone': 'North Coast South America',
     'next_port': 'Puerto Nuevo - Colombia',
     'next_berth': 'Puerto Nuevo Coal Pier',
     'next_operation': 'load'},

【问题讨论】:

  • 您需要显示数据文件的内容(或至少一个示例),以便人们调试问题。为了安全/清晰,您可能还不想用data(json)覆盖data(文件句柄)。

标签: python json pandas


【解决方案1】:

您已在索引中定义了 16 个元素,但您在 data 中提供给我们的内容不包含 16 个对象,至少我们可以看到它。

  1. 您可能需要阅读 pandas 文档以了解 .read_json() 方法:https://pandas.pydata.org/pandas-docs/version/0.24.2/reference/api/pandas.read_json.html
  2. 仔细检查源 JSON 文件,确保它的形状符合 pandas 可以使用的方式。您似乎正在尝试使用“拆分”格式的 JSON。

【讨论】:

    猜你喜欢
    • 2018-04-07
    • 2018-01-03
    • 2016-06-08
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 2020-03-08
    • 2018-11-17
    • 1970-01-01
    相关资源
    最近更新 更多