【问题标题】:Which Pandas function do I need? group_by or pivot我需要哪个 Pandas 功能? group_by 或 pivot
【发布时间】:2019-11-19 20:09:30
【问题描述】:

我对 Pandas 还比较陌生,我不知道我最适合使用哪些功能来获得答案。我已经查看了 pivot、pivot_table、group_by 和聚合,但我似乎无法让它完成我需要的工作。很可能是用户错误,对此我深表歉意!

我有这样的数据:

创建df的代码:

import pandas as pd
df = pd.DataFrame([
    ['1', '1', 'A', 3, 7],
    ['1', '1', 'B', 2, 9],
    ['1', '1', 'C', 2, 9],
    ['1', '2', 'A', 4, 10],
    ['1', '2', 'B', 4, 0],
    ['1', '2', 'C', 9, 8],
    ['2', '1', 'A', 3, 8],
    ['2', '1', 'B', 10, 4],
    ['2', '1', 'C', 0, 1],
    ['2', '2', 'A', 1, 6],
    ['2', '2', 'B', 10, 2],
    ['2', '2', 'C', 10, 3]
], columns = ['Field1', 'Field2', 'Type', 'Price1', 'Price2'])
print(df)

我正在尝试获取这样的数据:

虽然我的最终目标是最终为 A 列一列,为 B 列一列,为 C 列一列。因为 A 将使用 Price1 而 B & C 将使用 Price2。

我不希望获得价格的最大值或最小值或平均值或总和,因为理论上(尽管不太可能)对于相同的字段和类型可能有两个不同的 Price1。

在 Pandas 中使用什么功能可以满足我的需求?

【问题讨论】:

  • 请分享数据帧的样本而不是图像
  • 我不能提供实际数据作为其机密,我不是所有者,但这是数据的格式,尽管字段的层数不止 2 层。或者你是说你想要相同的数据,但我要提供将虚拟数据推送到这种格式的代码?
  • 我们想要的是可复制的数据。要么是一些 Python 代码来创建和填充数据帧(最好的,因为我们可以确定 dtypes),或者至少是 something 可以用pd.read_clipboard 复制和粘贴。你应该阅读How to make good reproducible pandas examples
  • 好的,谢谢,我会尽快编辑和添加。

标签: python pandas aggregate pandas-groupby


【解决方案1】:

使用pivot_table

pd.pivot_table(df, values =['Price1', 'Price2'], index=['Field1','Field2'],columns='Type').reset_index()

【讨论】:

    【解决方案2】:

    使用DataFrame.set_indexDataFrame.unstack 进行整形 - 列中的输出为MultiIndex,因此添加了按DataFrame.sort_index 进行第二级排序,展平值并最后从Field 级别创建列:

    df1 = (df.set_index(['Field1','Field2', 'Type'])
             .unstack(fill_value=0)
             .sort_index(axis=1, level=1))
    df1.columns = [f'{b}-{a}' for a, b in df1.columns]
    df1 = df1.reset_index()
    print (df1)
      Field1 Field2  A-Price1  A-Price2  B-Price1  B-Price2  C-Price1  C-Price2
    0      1      1         3         7         2         9         2         9
    1      1      2         4        10         4         0         9         8
    2      2      1         3         8        10         4         0         1
    3      2      2         1         6        10         2        10         3
    

    DataFrame.pivot_table 的解决方案也是可能的,但它使用默认的mean 函数聚合前 3 列中的重复值:

    df2 = (df.pivot_table(index=['Field1','Field2'],
                          columns='Type',
                          values=['Price1', 'Price2'],
                          aggfunc='mean')
             .sort_index(axis=1, level=1))
    df2.columns = [f'{b}-{a}' for a, b in df2.columns]
    df2 = df2.reset_index()
    print (df2)
    

    【讨论】:

    • 谢谢,我只是在用我的数据尝试它们,以确定哪一个给了我想要的东西,由于重复数据,它不喜欢 unstack,所以我只是想知道是否我首先需要或不需要那个副本。
    • @TimEdwards - 是的,如果重复,请使用第二种解决方案。
    • @TimEdwards - 另外mean 应更改为summedian,如您所愿。
    • 我想我可以使用 pivot_table 位,但是当我使用示例数据时,让列工作的部分给了我无效的语法。
    • 我通过使用df2.columns = ['-'.join(col).strip() for col in df2.columns.values]得到了重命名列位的工作
    猜你喜欢
    • 1970-01-01
    • 2021-12-18
    • 2014-09-28
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多