【问题标题】:Convert csv to json according to the requirements根据要求将csv转json
【发布时间】:2021-12-20 23:23:36
【问题描述】:

所以给出带有给定表格的 csv 文件:

标记:

test_id  student_id  mark
     1           1    78
     2           1    87
     3           1    95
     4           1    32
     5           1    65
     6           1    78
     7           1    40
     1           2    78
     2           2    87
     3           2    15
     6           2    78
     7           2    40
     1           3    78
     2           3    87
     3           3    95
     4           3    32
     5           3    65
     6           3    78
     7           3    40

课程:

   id name
   1    A
   2    B
   3    C

测试:

  id  course_id  weight
   1          1      10
   2          1      40
   3          1      50
   4          2      40
   5          2      60
   6          3      90
   7          3      10

学生:

  id name
   1    A
   2    B
   3    C

注意:权重:考试的价值占学生期末成绩的多少。例如,如果一个测试值 50,这意味着该测试值该课程最终成绩的 50%。

需要将它们转换成这种格式的json:

{
  "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
        }
      ]
    }
  ]
}

这类问题的新手,因此寻找如何从不同的表中获取值来计算 courseAverage 和 totalAverage。也在寻找如何将它相应地放入json中

【问题讨论】:

  • 如果您将这 4 个表格作为文本添加到您的问题中,您将在此处获得更好的帮助。另外,请展示您当前的尝试。这个问题看起来很像家庭作业,虽然我们可以提供帮助,但我们不是家庭作业写作服务
  • 添加了表格而不是作业
  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: python json pandas csv


【解决方案1】:

您的问题是非常典型的数据处理工作负载:组合来自各种来源的数据,执行一些计算并以定义的格式输出结果。

让我们首先定义输入数据帧:

from io import StringIO

marks = pd.read_csv(StringIO('''
test_id  student_id  mark
     1           1    78
     2           1    87
     3           1    95
     4           1    32
     5           1    65
     6           1    78
     7           1    40
     1           2    78
     2           2    87
     3           2    15
     6           2    78
     7           2    40
     1           3    78
     2           3    87
     3           3    95
     4           3    32
     5           3    65
     6           3    78
     7           3    40
'''), sep='\s+')

courses = pd.read_csv(StringIO('''
   id name
   1    A
   2    B
   3    C
'''), sep='\s+')

tests = pd.read_csv(StringIO('''
  id  course_id  weight
   1          1      10
   2          1      40
   3          1      50
   4          2      40
   5          2      60
   6          3      90
   7          3      10
'''), sep='\s+')

students = pd.read_csv(StringIO('''
  id name
   1    A
   2    B
   3    C
'''), sep='\s+')

及处理:

# Combine (aka join or merge) the 4 tables into one
# `id` has different meaning for each table so we will disambiguate
# it by renaming it `student_id`, `course_id`, etc.
combined = (
    students.add_prefix('student_')
        .merge(marks, on='student_id')
        .merge(tests.rename(columns={'id': 'test_id'}), on='test_id')
        .merge(courses.add_prefix('course_'), on='course_id')
)
combined['weighted_mark'] = combined['mark'] * combined['weight'] / 100


# Build a student-course level summary
# You didn't provide a `teacher_name` column in the `courses` dataframe
course_summary = (
    combined.groupby(['student_id', 'student_name', 'course_id', 'course_name'], as_index=False)
        .agg(course_average=('weighted_mark', 'sum'))
)
# Assemble the summary into a dictionary
course_summary['course'] = course_summary.apply(lambda row: {
    'id': row['course_id'],
    'name': row['course_name'],
    'courseAverage': round(row['course_average'], 1)
}, axis=1)


# Build a student-level summary
student_summary = (
    course_summary.groupby(['student_id', 'student_name'], as_index=False)
        .agg(
            # Aggregate all courses taken by each student into a list
            courses=('course', lambda c: list(c)),
            # Student's average is the mean of all course averages
            total_average=('course_average', 'mean')
        )
)
# Assemble the summary into a dictionary
student_summary['student'] = student_summary.apply(lambda row: {
    'id': row['student_id'],
    'name': row['student_name'],
    'totalAverage': round(row['total_average'], 2),
    'courses': row['courses']
}, axis=1)


# The final output:
import json
with open('output.json', 'w') as fp:
    json.dump({
        'students': student_summary['student'].to_list()
    }, fp, indent=2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 2015-08-23
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    相关资源
    最近更新 更多