python - Pandas:将分组的 df 转换为以两列作为键、值对的字典列表

标签 python python-2.7 pandas dictionary pandas-groupby

我有以下 df:

       YEAR      MONTH        VALUE
0   2010    january          1
1   2010   february          0
2   2010      march          2
3   2010      april          1
4   2010        may         -2
5   2010       june         -0
6   2010       july          1
7   2010     august          0
8   2010  september          1
9   2010    october          2
10  2010   november         -0
11  2010   december          0
12  2011    january          1
13  2011   february          0
14  2011      march          0
15  2011      april         -0
16  2011        may          0
17  2011       june         -0
18  2011       july         -0
19  2011     august         -1
20  2011  september         -1
21  2011    october          1
22  2011   november          0
23  2011   december          1

我需要将其转换为以下格式

[{"id":0,"year":2010,"january":1,"february":1,"march":2,"april":1,"may":null,"june":null,"july":null,"august":null,"september":null,"october":null,"november":null,"december":null

基本上我已经按年份对 df 进行了分组。现在我想要每个组有一个字典,其中月份作为键,其相应的值作为值。有一个额外的键、年份值和组号 (id=0)

PS:忽略我想要的格式中的空值。它们都应该有相应的月份值

最佳答案

您只需调用 dict(df.values) 即可根据值创建字典,然后只需以正确的方式链接组即可构建列表。

out = []
for idx, (key, group) in enumerate(df.groupby('YEAR')):
    year = dict(group.iloc[:, ~group.columns.isin(['YEAR'])].values)
    year.update({'id': idx})
    out.append(year)

或者作为列表理解。

dict_merge = lambda a,b: a.update(b) or a
out = [dict_merge(dict(group.iloc[:, 1:].values), {'id': idx}) for idx, (key, group) in enumerate(groups)]
print(out)
[{'april': 1.56,
  'august': 0.95,
  'december': 0.83,
  'february': 0.81,
  'id': 0,
  'january': 1.02,
  'july': 1.32,
  'june': -0.57,
  'march': 2.66,
  'may': -2.02,
  'november': -0.53,
  'october': 2.17,
  'september': 1.79},
 {'april': -0.17,
  'august': -1.81,
  'december': 1.36,
  'february': 0.84,
  'id': 1,
  'january': 1.06,
  'july': -0.04,
  'june': -0.27,
  'march': 0.11,
  'may': 0.15,
  'november': 0.75,
  'october': 1.95,
  'september': -1.55}]

关于python - Pandas:将分组的 df 转换为以两列作为键、值对的字典列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49521810/

相关文章:

python - 在 python 中将多个列表写入 JSON 文件

python - pandas多索引选择: How to select subset of a dataframe?

Python 2.7 字符串解码。

python - 将有序字典修剪到最后 x 项

python - 根据另一列更改列的值

python - 更新数据框 ID w.r.t 缺少日期列值

python - 如何清除 python/django 中的代码缓存?

具有 4000 个独特类别的大文件上的 Python 数据透视表

python-2.7 - pytest-timeit或pytest-benchmark在精度s上哪个更好?

python - 如何将缺失行的记录添加到 Dataframe