pandas to_json 排除 groupby 键

pandas to_json exclude the groupby keys

我们如何从 to_json 方法中排除按键分组?

import pandas as pd

students_df = pd.DataFrame(
    [
        ["Jay", 16, "Soccer"],
        ["Jack", 19, "FootBall"],
        ["Dorsey", 19, "Dining"],
        ["Mark", 18, "Swimming"],
    ],
    columns=["Name", "Age", "Sport"],
)
students_df.groupby("Name").apply(lambda x: x.to_json(orient="records")).reset_index(
    name="students_json"
)

当前输出:

    Name                                  students_json
0  Dorsey  [{"Name":"Dorsey","Age":19,"Sport":"Dining"}]
1    Jack  [{"Name":"Jack","Age":19,"Sport":"FootBall"}]
2     Jay     [{"Name":"Jay","Age":16,"Sport":"Soccer"}]
3    Mark  [{"Name":"Mark","Age":18,"Sport":"Swimming"}]

我想从结果 json 中排除按键分组。

可能有多个键,我可以在这些键上进行分组,而不仅仅是名称。 预期输出应为:

     Name                                  students_json
0  Dorsey                                   [{"Age":19,"Sport":"Dining"}]
1    Jack                                   [{"Age":19,"Sport":"FootBall"}]
2     Jay                                   [{"Age":16,"Sport":"Soccer"}]
3    Mark                                   [{"Age":18,"Sport":"Swimming"}]

你可以drop它:

out = students_df.groupby('Name').apply(lambda x: x.drop(columns='Name').to_json(orient="records"))

输出:

Name
Dorsey      [{"Age":19,"Sport":"Dining"}]
Jack      [{"Age":19,"Sport":"FootBall"}]
Jay         [{"Age":16,"Sport":"Soccer"}]
Mark      [{"Age":18,"Sport":"Swimming"}]
dtype: object

在 json 中指定您想要的列。

students_df.groupby("Name").apply(
lambda x: x[["Age", "Sport"]].to_json(orient="records")).reset_index(name="students_json")

     Name                    students_json
0  Dorsey    [{"Age":19,"Sport":"Dining"}]
1    Jack  [{"Age":19,"Sport":"FootBall"}]
2     Jay    [{"Age":16,"Sport":"Soccer"}]
3    Mark  [{"Age":18,"Sport":"Swimming"}]