【问题标题】:How to convert pandas Data frame into Json?如何将熊猫数据框转换为 Json?
【发布时间】:2023-03-18 11:09:01
【问题描述】:

我正在使用 pandas,并且我在数据框上应用了 groupby。现在我需要将此数据帧转换为 Json 文件。

数据框(名称 = 分组):

student_id stuent_name course_id subject_name teacher weight_score total_average
1 A 1 Biology Mr.D 90.1 72.0333
2 History Mrs.P 51.8 72.0333
3 Math Mrs.C 74.2 72.0333
2 B 1 Biology Mr.D 50.1 62.1500
3 Math Mrs.C 74.2 62.1500
3 C 1 Biology Mr.D 90.1 72.0333
2 History Mrs.P 51.8 72.0333
3 Math Mrs.C 74.2 72.0333

数据帧类型:

type(grouped)
pandas.core.frame.DataFrame

当我尝试检索列时,它只允许我获取两列。我想要所有带有列名的值。

grouped.columns
Index(['weighted_score', 'total_average'], dtype='object')

我需要这样的输出:

[{
  "students": [
    {
      "id": 1,
      "name": "A",
      "totalAverage": 72.03,
      "courses": [
        {
          "id": 1,
          "name": "Biology",
          "teacher": "Mr. D",
          "courseAverage": 90.1
        },
        {
          "id": 3,
          "name": "Math",
          "teacher": "Mrs. C",
          "courseAverage": 74.2
        },
        {
          "id": 2,
          "name": "History",
          "teacher": "Mrs. P",
          "courseAverage": 51.8
        }
      ]
    },
    {
      "id": 2,
      "name": "B",
      "totalAverage": 62.15,
      "courses": [
        {
          "id": 1,
          "name": "Biology",
          "teacher": "Mr. D",
          "courseAverage": 50.1
        },
        {
          "id": 3,
          "name": "Math",
          "teacher": "Mrs. C",
          "courseAverage": 74.2
        }
      ]
    },
    {
      "id": 3,
      "name": "C",
      "totalAverage": 72.03,
      "courses": [
        {
          "id": 1,
          "name": "Biology",
          "teacher": "Mr. D",
          "courseAverage": 90.1
        },
        {
          "id": 2,
          "name": "History",
          "teacher": "Mrs. P",
          "courseAverage": 51.8
        },
        {
          "id": 3,
          "name": "Math",
          "teacher": "Mrs. C",
          "courseAverage": 74.2
        }
      ]
    }
  ]
}]

【问题讨论】:

    标签: python json pandas pandas-groupby data-analysis


    【解决方案1】:

    一种方式:

    df1 = df.reset_index()
    new_dict = (
        df1.set_index(['student_id', 'stuent_name', 'total_average'])
        .apply(dict, 1)
        .groupby(level=[0, 1, 2])
        .agg(list)
        .reset_index(name='courses')
        .to_dict('records')
    )
    

    输出:

    [{'student_id': 1,
      'stuent_name': 'A',
      'total_average': 72.0333,
      'courses': [{'course_id': '1',
        'subject_name': 'Biology',
        'teacher': 'Mr.D',
        'weight_score': 90.1},
       {'course_id': '1',
        'subject_name': 'History',
        'teacher': 'Mrs.P',
        'weight_score': 51.8},
       {'course_id': '1',
        'subject_name': 'Math',
        'teacher': 'Mrs.C',
        'weight_score': 74.2}]},
     {'student_id': 2,
      'stuent_name': 'B',
      'total_average': 62.15,
      'courses': [{'course_id': '1',
        'subject_name': 'Biology',
        'teacher': 'Mr.D',
        'weight_score': 50.1},
       {'course_id': '1',
        'subject_name': 'Math',
        'teacher': 'Mrs.C',
        'weight_score': 74.2}]},
     {'student_id': 3,
      'stuent_name': 'C',
      'total_average': 72.0333,
      'courses': [{'course_id': '1',
        'subject_name': 'Biology',
        'teacher': 'Mr.D',
        'weight_score': 90.1},
       {'course_id': '1',
        'subject_name': 'History',
        'teacher': 'Mrs.P',
        'weight_score': 51.8},
       {'course_id': '1',
        'subject_name': 'Math',
        'teacher': 'Mrs.C',
        'weight_score': 74.2}]}]
    

    【讨论】:

      猜你喜欢
      • 2021-08-21
      • 2021-04-19
      • 2018-08-25
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 2020-10-18
      相关资源
      最近更新 更多