【问题标题】:Python dataframe manupulationPython数据框操作
【发布时间】:2020-12-07 13:39:17
【问题描述】:

我正在尝试将以下输入数据帧转换为输出数据帧

import pandas as pd

data = {'Model1': [86,23,32,13,45,12],
        'Model2': [96,98,34,12,22,19], 
        'Model3': [56,23,44,12,32,33]
       }

Input = pd.DataFrame(data, 
                     columns=['Model1','Model2','Model3'], 
                     index=['I1', 'I2','I3','I4','I5','I6'])

Output = pd.DataFrame(data={'Best Model': ['Model2','Model2', 'Model3','Model1', 'Model1', 'Model3'],
                            'Best Model Accuracy': [96,98,44,13,45,33]}, 
                      columns=['Best Model','Best Model Accuracy'], 
                      index=['I1', 'I2','I3','I4','I5','I6'])

逻辑:我有 6 个客户的 3 个模型准确度结果,我想为每个客户选择准确度最好的模型。最佳模型是指对该客户具有最高准确度的模型。

我能够做每一个的支点,但坚持为每个客户逻辑找到最佳准确性

【问题讨论】:

    标签: python pandas numpy dataframe sklearn-pandas


    【解决方案1】:

    您可以使用idxmaxlookup

    idx = Input.idxmax(1)
    output = pd.DataFrame({'Best Model':idx, 
                           'Best Acc':Input.lookup(Input.index, idx)
                         })
    

    输出:

       Best Model  Best Acc
    I1     Model2        96
    I2     Model2        98
    I3     Model3        44
    I4     Model1        13
    I5     Model1        45
    I6     Model3        33
    

    【讨论】:

    • 通过构造Input.lookup(Input.index, idx) 等价于Input.max(axis=1)。所以输出可以写成:output = pd.DataFrame({' Best Model':Input.idxmax(1), 'Best Acc':Input.max(axis=1) })
    • @dberezin 是的,实际上现在 lookup 已被弃用会更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2018-06-21
    • 2019-01-05
    • 1970-01-01
    • 2022-11-24
    • 2017-11-30
    • 2013-03-04
    相关资源
    最近更新 更多