【问题标题】:convert dataframe with multiple index as keys of keys with value of columns将具有多个索引的数据框转换为具有列值的键的键
【发布时间】:2021-07-14 04:28:07
【问题描述】:

我的项目的用例是显示每个作者的提交以及每天每个提交的大小。这就是我需要表示我的数据的方式

为此,我所做的是

timed_commits = commit_data.set_index('Date')
grouped = timed_commits.groupby(by=["Author"])
resampled = grouped.resample("D").agg(
            {"SHA": "size", "Insertion": "sum", "Deletion": "sum"}
        ) # get the total count of commits in a day with total insertion and deletion

这给了我下面的输出

这里作者和日期是索引,而 SHA、插入和删除是列。 Author 和 Date 是索引的原因是我想知道每个作者每天的提交,同时我也想知道每个提交的大小(通过插入)

对于这样的对象,我无法以这种方式或任何其他方式格式化(最好有作者值和日期值的字段名称),这将支持在表格中显示,如我为它附上的图片

{
    'author1': {
        '2017-10-18': {'SHA': 1, 'Insertion': 1.0, 'Deletion': 3.0},
        '2017-10-19': {'SHA': 2, 'Insertion': 1.0, 'Deletion': 3.0},
        '2017-10-20': {'SHA': 6, 'Insertion': 1.0, 'Deletion': 3.0},
        '2017-10-21': {'SHA': 9, 'Insertion': 1.0, 'Deletion': 3.0},
    },
    'author2': {
        '2017-10-18': {'SHA': 3, 'Insertion': 8.0, 'Deletion': 3.0},
        '2017-10-19': {'SHA': 19, 'Insertion': 10.0, 'Deletion': 3.0},
        '2017-10-20': {'SHA': 23, 'Insertion': 1.0, 'Deletion': 3.0},
        '2017-10-21': {'SHA': 44, 'Insertion': 1.0, 'Deletion': 3.0},
    }
}

我玩过 to_dict,但没有让它可行。

这是数据帧(这里的提交哈希,即 sha 由于该特定提交中更改的文件数量而重复)。这是从 git 日志中获取的。

SHA Timestamp   Date    Author  Insertion   Deletion    Churn   File path
1   cae635054   Sat Jun 26 14:51:23 2021 -0400  2021-06-26 18:51:23+00:00   Andrew Clark    31.0    0.0 31.0    packages/react-reconciler/src/__tests__/ReactI...
2   cae635054   Sat Jun 26 14:51:23 2021 -0400  2021-06-26 18:51:23+00:00   Andrew Clark    1.0 1.0 0.0 packages/react-test-renderer/src/ReactTestRend...
3   cae635054   Sat Jun 26 14:51:23 2021 -0400  2021-06-26 18:51:23+00:00   Andrew Clark    24.0    14.0    10.0    packages/react/src/ReactAct.js
5   e2453e200   Fri Jun 25 15:39:46 2021 -0400  2021-06-25 19:39:46+00:00   Andrew Clark    50.0    0.0 50.0    packages/react-reconciler/src/__tests__/ReactI...
7   73ffce1b6   Thu Jun 24 22:42:44 2021 -0400  2021-06-25 02:42:44+00:00   Brian Vaughn    4.0 5.0 -1.0    packages/react-devtools-shared/src/__tests__/F...
8   73ffce1b6   Thu Jun 24 22:42:44 2021 -0400  2021-06-25 02:42:44+00:00   Brian Vaughn    4.0 4.0 0.0 packages/react-devtools-shared/src/__tests__/c...
9   73ffce1b6   Thu Jun 24 22:42:44 2021 -0400  2021-06-25 02:42:44+00:00   Brian Vaughn    12.0    12.0    0.0 packages/react-devtools-shared/src/__tests__/c...
10  73ffce1b6   Thu Jun 24 22:42:44 2021 -0400  2021-06-25 02:42:44+00:00   Brian Vaughn    7.0 6.0 1.0 packages/react-devtools-shared/src/__tests__/e...
11  73ffce1b6   Thu Jun 24 22:42:44 2021 -0400  2021-06-25 02:42:44+00:00   Brian Vaughn    47.0    42.0    5.0 packages/react-devtools-shared/src/__tests__/i...
12  73ffce1b6   Thu Jun 24 22:42:44 2021 -0400  2021-06-25 02:42:44+00:00   Brian Vaughn    7.0 6.0 1.0 packages/react-devtools-shared/src/__tests__/o...

【问题讨论】:

  • 您可以添加代码来创建示例 df 吗?使用df.head().to_dict()。此外,检查 -> stackoverflow.com/questions/20109391/…
  • 你的意思是说resampled.head().to_dict()就我而言?
  • 是的,使用您的数据框的名称。我猜是commit_data
  • 已更新。让我知道这是否是预期的。

标签: python python-3.x pandas dataframe dictionary


【解决方案1】:

也许我完全误解了。

我假设您要转换 DataFrame df

                    SHA  Insertion  Deletion
Author  Date                                
author1 2017-10-18    1        1.0       3.0
        2017-10-19    2        1.0       3.0
        2017-10-20    6        1.0       3.0
        2017-10-21    9        1.0       3.0
author2 2017-10-18    3        8.0       3.0
        2017-10-19   19       10.0       3.0
        2017-10-20   23        1.0       3.0
        2017-10-21   44        1.0       3.0

进入您提供的dict-格式?

如果是这样,那么试试这个:

result = {
    key: group.reset_index(level=0, drop=True).to_dict(orient='index')
    for key, group in df.groupby('Author')
}

或者这个

result = (df.groupby('Author')
            .apply(lambda sdf: sdf.reset_index(level=0, drop=True).to_dict(orient='index'))
            .to_dict())

样本结果:

{'author1': {'2017-10-18': {'Deletion': 3.0, 'Insertion': 1.0, 'SHA': 1},
             '2017-10-19': {'Deletion': 3.0, 'Insertion': 1.0, 'SHA': 2},
             '2017-10-20': {'Deletion': 3.0, 'Insertion': 1.0, 'SHA': 6},
             '2017-10-21': {'Deletion': 3.0, 'Insertion': 1.0, 'SHA': 9}},
 'author2': {'2017-10-18': {'Deletion': 3.0, 'Insertion': 8.0, 'SHA': 3},
             '2017-10-19': {'Deletion': 3.0, 'Insertion': 10.0, 'SHA': 19},
             '2017-10-20': {'Deletion': 3.0, 'Insertion': 1.0, 'SHA': 23},
             '2017-10-21': {'Deletion': 3.0, 'Insertion': 1.0, 'SHA': 44}}}

编辑:@milan 使用的另一个版本:

result = [
    {
         "author": key,
         "commit_activity": group.to_dict(orient="records"),
         "timestamp": [index[1] for index in list(group.index)]
    }
    for key, group in df.groupby("Author")
]

此版本的结果如下所示:

[

    {'author': 'Aaron Pettengill',
      'commit_activity': [{'SHA': 2,
        'Insertion': 156.0,
        'Deletion': 8.0,
        'File path': 2}],
      'timestamp': [Timestamp('2020-05-01 00:00:00+0000', tz='UTC')]},
     {'author': 'Alex Rohleder',
      'commit_activity': [{'SHA': 5,
        'Insertion': 5.0,
        'Deletion': 5.0,
        'File path': 5}],
      'timestamp': [Timestamp('2019-09-06 00:00:00+0000', tz='UTC')]},
     {'author': 'Alex Taylor',
      'commit_activity': [{'SHA': 2,
        'Insertion': 30.0,
        'Deletion': 3.0,
        'File path': 2}],
      'timestamp': [Timestamp('2020-04-29 00:00:00+0000', tz='UTC')]}
]

【讨论】:

  • 非常感谢您的帮助。这正是我想要的。我希望响应准备好 json,以便我可以将其发送给客户端以显示在表格中。我会将其标记为已解决。如果这个是发送给客户的更好方法,你能建议我吗?只需提出建议即可。
  • @milan 感谢您的反馈。关于您的问题:不幸的是,这不是我拥有可靠专业知识的领域:(我的直觉是直接发送 json 会更好。但正如我所说,这可能是我的误判。
  • 我也以另一种方式做到了。 result = [ # key: group.reset_index(level=0, drop=True).to_dict(orient='index') { "author": key, "commitInfo": group.to_dict(orient="records"), "timestamp": [index[1] for index in list(group.index)], } for key, group in work_logs.groupby("author") ]如果你没问题,那你也可以放这个case。再次感谢您的帮助。
  • @milan 当然!我已将您的解决方案添加到答案中。如果我犯了错误或您想要调整,请随时直接编辑。
  • 已更新结果。
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 2019-02-11
  • 2019-05-05
  • 1970-01-01
  • 2023-04-03
  • 2022-01-07
  • 1970-01-01
  • 2018-06-24
相关资源
最近更新 更多