【问题标题】:Get Row data associated with max value after pivoting the df using pandas使用 pandas 旋转 df 后获取与最大值关联的行数据
【发布时间】:2021-08-25 19:23:47
【问题描述】:

我有一个看起来像这样的熊猫数据框。

import pandas as pd

df = pd.DataFrame({
    'col1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
    'col2': ['test1', 'test1', 'test1', 'test1', 'test2', 'test2', 'test2',
             'test2', 'test3', 'test3', 'test3', 'test3', 'test4', 'test5',
             'test1', 'test1'],
    'col3': ['t1', 't1', 't1', 't1', 't1', 't1', 't1', 't1', 't1', 't1', 't1',
             't1', 't1', 't1', 't1', 't1'],
    'col4': ['input1', 'input2', 'input3', 'input4', 'input1', 'input2',
             'input3', 'input4', 'input1', 'input2', 'input3', 'input5',
             'input2', 'input6', 'input1', 'input1'],
    'col5': ['result1', 'result2', 'result3', 'result4', 'result1', 'result2',
             'result3', 'result4', 'result1', 'result2', 'result3', 'result4',
             'result2', 'result1', 'result2', 'result6'],
    'col6': [10, 20, 30, 40, 10, 20, 30, 40, 10, 20, 30, 50, 20, 100, 10, 10],
    'col7': [100.2, 101.2, 102.3, 101.4, 100.0, 103.0, 104.0, 105.0, 102.0,
             87.0, 107.0, 110.2, 120.0, 88.0, 106.2, 101.1]
})

ptable = df.pivot_table(values='col7', index=['col4'], columns=['col2'], aggfunc='max')

ptable = (ptable [(ptable >= 100).any(1)]
        .fillna('')
        .reset_index()
        .rename_axis(None, axis=1))

一旦数据在 col2 上旋转,我正在尝试检索与最大值关联的行数据。我感兴趣的数据是 col5 和 col6 数据,但无法弄清楚如何获取该信息。非常感谢任何意见。

我要找的两个输出表如下:

请求的输出表#1:

col4 col5 col6 test1 test2 test3 test4 test5
input1 result2 10 106.2
input1 result1 10 100 102
input2 result2 20 101.2 103 87 120
input3 result3 30 102.3 104 107
input4 result4 40 101.4 105
input5 result4 50 110.2

请求的输出表#2:

col4 col5 col6 test1 test2 test3 test4 test5
input1 result2 10 106.2
input1 result1 10 102.2 100 102
input2 result2 20 101.2 103 87 120
input3 result3 30 102.3 104 107
input4 result4 40 101.4 105
input5 result4 50 110.2

提前致谢。

【问题讨论】:

    标签: python pandas dataframe pivot-table


    【解决方案1】:

    更新包括大于 100:

    ptable = df.pivot_table(values='col7', index=['col4', 'col5', 'col6'], columns=['col2'], aggfunc='max')
    ptable_1 = ptable.where((ptable.groupby(level=0).rank(ascending=False) == 1.)&(ptable>100)).dropna(how='all')
    print(ptable_1)
    

    输出:

    col2                 test1  test2  test3  test4  test5
    col4   col5    col6                                   
    input1 result1 10      NaN    NaN  102.0    NaN    NaN
           result2 10    106.2    NaN    NaN    NaN    NaN
    input2 result2 20    101.2  103.0    NaN  120.0    NaN
    input3 result3 30    102.3  104.0  107.0    NaN    NaN
    input4 result4 40    101.4  105.0    NaN    NaN    NaN
    input5 result4 50      NaN    NaN  110.2    NaN    NaN
    

    还有,

    ptable_2 = ptable[((ptable.groupby(level=0).rank(ascending=False) == 1.).any(axis=1))&(ptable>100).any(axis=1)]
    print(ptable_2
    

    输出:

    col2                 test1  test2  test3  test4  test5
    col4   col5    col6                                   
    input1 result1 10    100.2  100.0  102.0    NaN    NaN
           result2 10    106.2    NaN    NaN    NaN    NaN
    input2 result2 20    101.2  103.0   87.0  120.0    NaN
    input3 result3 30    102.3  104.0  107.0    NaN    NaN
    input4 result4 40    101.4  105.0    NaN    NaN    NaN
    input5 result4 50      NaN    NaN  110.2    NaN    NaN
    

    IIUC,试试吧:

    ptable = df.pivot_table(values='col7', index=['col4', 'col5', 'col6'], columns=['col2'], aggfunc='max')
    ptable_1 = ptable.where(ptable.groupby(level=0).rank(ascending=False) == 1.).dropna(how='all')
    print(ptable_1)
    

    输出:

    col2                 test1  test2  test3  test4  test5
    col4   col5    col6                                   
    input1 result1 10      NaN  100.0  102.0    NaN    NaN
           result2 10    106.2    NaN    NaN    NaN    NaN
    input2 result2 20    101.2  103.0   87.0  120.0    NaN
    input3 result3 30    102.3  104.0  107.0    NaN    NaN
    input4 result4 40    101.4  105.0    NaN    NaN    NaN
    input5 result4 50      NaN    NaN  110.2    NaN    NaN
    input6 result1 100     NaN    NaN    NaN    NaN   88.0
    

    还有,

    ptable_2 = ptable[(ptable.groupby(level=0).rank(ascending=False) == 1.).any(axis=1)]
    print(ptable_2)
    

    输出:

    col2                 test1  test2  test3  test4  test5
    col4   col5    col6                                   
    input1 result1 10    100.2  100.0  102.0    NaN    NaN
           result2 10    106.2    NaN    NaN    NaN    NaN
    input2 result2 20    101.2  103.0   87.0  120.0    NaN
    input3 result3 30    102.3  104.0  107.0    NaN    NaN
    input4 result4 40    101.4  105.0    NaN    NaN    NaN
    input5 result4 50      NaN    NaN  110.2    NaN    NaN
    input6 result1 100     NaN    NaN    NaN    NaN   88.0
    

    【讨论】:

    • 感谢您的解决方案。如果 col7 大于 100,如何输入仅报告 col7 的条件。
    • 感谢它完美运行。一个后续问题,有没有办法用“Col4 数据丢失”或“Col5 数据丢失”或“Col4 和 Col5 数据丢失”填充 NaN。
    • @buggsbunny4 我不遵循这里的逻辑。从输入数据框中为不存在的数据添加 nan?
    • 我的意思是,我想知道是否可以用某些东西替换最终输出表中的“NaN”,而不是用 NaN/空白填充输出表根据具体情况,例如“缺少 Col4 数据”或“缺少 Col5 数据”或“缺少 Col4 和 Col5 数据”。我想做的是告诉最终用户,为什么有些单元格是空白的,并向他们提供缺少的组合。例如对于输出表的第一行,test4 列的 input1-result1 行,而不是说“NaN”,我会说“Col4 & Col5 data is missing”
    • @buggsbunny4 在这种情况下,所有值都是“缺少 Col4 和 Col5 数据”这种特殊的数据结构不允许仅缺少 Col4 或 Col5 的 NaN,我不认为.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    相关资源
    最近更新 更多