在熊猫中创建一个带有多个列的嵌套词典

发布于 2025-02-12 20:09:33 字数 345 浏览 1 评论 0原文

我正在尝试以这种方式对数据进行分组 - {10:{10:[POL],5:[CARL]}

现在,我已经根据年龄和数据列对数据进行了分组。现在,我也试图将评分包括在其中。因此{年龄:{评分:[数据],评分:[data]}

这是我现在分组的方式,

df.groupby(“ age”)[“ data”]。agg(list).to_dict()

[table )链接] https://i.sstatic.net/pc8cw.png

I'm trying to group the data in this way - {10:{10:[Pole], 5:[Carl]}

Right now, I have grouped data based on age and data column. Now I'm trying to include rating in it as well. So {Age:{Rating:[Data], Rating:[Data]}

This is how I'm grouping now,

df.groupby("Age")["Data"].agg(list).to_dict()

[Table Link] https://i.sstatic.net/PC8Cw.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

要走就滚别墨迹 2025-02-19 20:09:33

分组可以由Groupby完成,但是构建字典只能通过自定义功能完成。 create_dict用于基于分组键动态创建嵌套词典,其中最内在的元素设置了值

import pandas as pd


def create_dict(key_lst, val):
    global res
    key_num = len(key_lst)
    tmp_dict = res
    for index, key in enumerate(key_lst):
        if index == key_num - 1:
            tmp_dict[key] = val
        else:
            if key not in tmp_dict:
                tmp_dict.setdefault(key, {})

        tmp_dict = tmp_dict[key]


res = {}
df = pd.DataFrame({"Age": [10, 10, 3], "Data": ["Pole", "Carl", "Jack"], "Rating": [10, 5, 5]})

group_data = df.groupby(["Age", "Rating"]).agg({"Data": list}).Data

for k, v in zip(group_data.index, group_data.values):
    create_dict(k, v)

print(res)

# {3: {5: ['Jack']}, 10: {5: ['Carl'], 10: ['Pole']}}

Grouping can be done by groupby, but building dictionaries can only be done by custom functions. create_dict is used to dynamically create nested dictionaries based on grouping keys, with the innermost element setting the value

import pandas as pd


def create_dict(key_lst, val):
    global res
    key_num = len(key_lst)
    tmp_dict = res
    for index, key in enumerate(key_lst):
        if index == key_num - 1:
            tmp_dict[key] = val
        else:
            if key not in tmp_dict:
                tmp_dict.setdefault(key, {})

        tmp_dict = tmp_dict[key]


res = {}
df = pd.DataFrame({"Age": [10, 10, 3], "Data": ["Pole", "Carl", "Jack"], "Rating": [10, 5, 5]})

group_data = df.groupby(["Age", "Rating"]).agg({"Data": list}).Data

for k, v in zip(group_data.index, group_data.values):
    create_dict(k, v)

print(res)

# {3: {5: ['Jack']}, 10: {5: ['Carl'], 10: ['Pole']}}

走过海棠暮 2025-02-19 20:09:33

尝试以下操作:

df.set_index('Rating').groupby(["Age"]).agg(dict).apply(lambda x: x.to_dict())

输出:

Data    {3: {5: 'Jack'}, 10: {10: 'Pole', 5: 'Carl'}}

对于不同的输入:

Age Data    Rating
10  Pole    10
10  Carl    10
3   Jack    5

结果:

{3: {5: 'Jack'}, 10: {10: ['Pole', 'Carl']}}

我不确定这就是您要寻找的。可能删除:)

try this:

df.set_index('Rating').groupby(["Age"]).agg(dict).apply(lambda x: x.to_dict())

output:

Data    {3: {5: 'Jack'}, 10: {10: 'Pole', 5: 'Carl'}}

for different input:

Age Data    Rating
10  Pole    10
10  Carl    10
3   Jack    5

result:

{3: {5: 'Jack'}, 10: {10: ['Pole', 'Carl']}}

I'm, not sure this is what you're looking for. might delete :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文