词典列表列表

发布于 2025-02-11 20:16:54 字数 612 浏览 0 评论 0原文

我有以下df:

df = pd.DataFrame({"year":[2020,2020,2020,2021,2021,2021,2022,2022, 2022],"region":['europe','USA','africa','europe','USA','africa','europe','USA','africa'],'volume':[1,6,5,3,8,7,6,3,5]})

”“在此处输入图像描述”

我希望将其转换为字典列表,以便每项仅在每个项目中提及一次。例如

[{'year':2020,'europe':1,'USA':6,'africa':5,}...]

df.set_index('year').to_dict('records')

我丢失了岁月和清单

I have the following df:

df = pd.DataFrame({"year":[2020,2020,2020,2021,2021,2021,2022,2022, 2022],"region":['europe','USA','africa','europe','USA','africa','europe','USA','africa'],'volume':[1,6,5,3,8,7,6,3,5]})

enter image description here

I wish to convert it to a list of dictionary such that the year would be mentioned only once in each item. Example

[{'year':2020,'europe':1,'USA':6,'africa':5,}...]

when I do:

df.set_index('year').to_dict('records')

I lost the years and the list

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

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

发布评论

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

评论(4

愚人国度 2025-02-18 20:16:54

to_dict(orient ='records')之前使用枢轴的另一种方法

df.pivot(
    index='year',
    columns='region',
    values='volume'
).reset_index().to_dict(orient='records')

#Output:
#[{'year': 2020, 'USA': 6, 'africa': 5, 'europe': 1},
# {'year': 2021, 'USA': 8, 'africa': 7, 'europe': 3},
# {'year': 2022, 'USA': 3, 'africa': 5, 'europe': 6}]

Another approach that uses pivot before to_dict(orient='records')

df.pivot(
    index='year',
    columns='region',
    values='volume'
).reset_index().to_dict(orient='records')

#Output:
#[{'year': 2020, 'USA': 6, 'africa': 5, 'europe': 1},
# {'year': 2021, 'USA': 8, 'africa': 7, 'europe': 3},
# {'year': 2022, 'USA': 3, 'africa': 5, 'europe': 6}]
红墙和绿瓦 2025-02-18 20:16:54

尝试:

d = [
    {"year": y, **dict(zip(x["region"], x["volume"]))}
    for y, x in df.groupby("year")
]

print(d)

打印:

[
    {"year": 2020, "europe": 1, "USA": 6, "africa": 5},
    {"year": 2021, "europe": 3, "USA": 8, "africa": 7},
    {"year": 2022, "europe": 6, "USA": 3, "africa": 5},
]

Try:

d = [
    {"year": y, **dict(zip(x["region"], x["volume"]))}
    for y, x in df.groupby("year")
]

print(d)

Prints:

[
    {"year": 2020, "europe": 1, "USA": 6, "africa": 5},
    {"year": 2021, "europe": 3, "USA": 8, "africa": 7},
    {"year": 2022, "europe": 6, "USA": 3, "africa": 5},
]
痴骨ら 2025-02-18 20:16:54

您可以一年使用Groupby,然后可以使用邮政区和音量

import pandas as pd

df = pd.DataFrame({"year":[2020,2020,2020,2021,2021,2021,2022,2022, 2022],"region":['europe','USA','africa','europe','USA','africa','europe','USA','africa'],'volume':[1,6,5,3,8,7,6,3,5]})

year_dfs = df.groupby("year")
records = []
for year, year_df in year_dfs:
    year_dict = {key: value for key, value in zip(year_df["region"], year_df["volume"])}
    year_dict["year"] = year
    records.append(year_dict)
""" Answer
[{'europe': 1, 'USA': 6, 'africa': 5, 'year': 2020},
 {'europe': 3, 'USA': 8, 'africa': 7, 'year': 2021},
 {'europe': 6, 'USA': 3, 'africa': 5, 'year': 2022}]
"""

you can use groupby on year and then zip region and volume

import pandas as pd

df = pd.DataFrame({"year":[2020,2020,2020,2021,2021,2021,2022,2022, 2022],"region":['europe','USA','africa','europe','USA','africa','europe','USA','africa'],'volume':[1,6,5,3,8,7,6,3,5]})

year_dfs = df.groupby("year")
records = []
for year, year_df in year_dfs:
    year_dict = {key: value for key, value in zip(year_df["region"], year_df["volume"])}
    year_dict["year"] = year
    records.append(year_dict)
""" Answer
[{'europe': 1, 'USA': 6, 'africa': 5, 'year': 2020},
 {'europe': 3, 'USA': 8, 'africa': 7, 'year': 2021},
 {'europe': 6, 'USA': 3, 'africa': 5, 'year': 2022}]
"""
农村范ル 2025-02-18 20:16:54

要分解每个步骤,您可以使用Pivot将DF分组以汇总几年,您的列成为国家,并且数量成为您的价值观,

df.pivot('year','region','volume')

region  USA  africa  europe
year                       
2020      6       5       1
2021      8       7       3
2022      3       5       6

将其变成字典格式,您可以使用.to_dict('index')
命令(一行)

x = df.pivot('year','region','volume').to_dict('index')

{2020: {'USA': 6, 'africa': 5, 'europe': 1}, 2021: {'USA': 8, 'africa': 7, 'europe': 3}, 2022: {'USA': 3, 'africa': 5, 'europe': 6}}

最后您可以使用列表理解将其纳入所需格式

output = [dict(x[y], **{'year':y}) for y in x]
[{'USA': 6, 'africa': 5, 'europe': 1, 'year': 2020}, {'USA': 8, 'africa': 7, 'europe': 3, 'year': 2021}, {'USA': 3, 'africa': 5, 'europe': 6, 'year': 2022}]

To break down each step, you could use pivot to group your df to aggregate the years, your columns become countries, and volume becomes your values

df.pivot('year','region','volume')

region  USA  africa  europe
year                       
2020      6       5       1
2021      8       7       3
2022      3       5       6

To get this into dictionary format you can use the .to_dict('index')
command (in one line)

x = df.pivot('year','region','volume').to_dict('index')

{2020: {'USA': 6, 'africa': 5, 'europe': 1}, 2021: {'USA': 8, 'africa': 7, 'europe': 3}, 2022: {'USA': 3, 'africa': 5, 'europe': 6}}

finally you could use list comprehension to get it into your desired format

output = [dict(x[y], **{'year':y}) for y in x]
[{'USA': 6, 'africa': 5, 'europe': 1, 'year': 2020}, {'USA': 8, 'africa': 7, 'europe': 3, 'year': 2021}, {'USA': 3, 'africa': 5, 'europe': 6, 'year': 2022}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文