Python 从列A中获取列名,然后将该列的值保存到新的列C中
我有一个熊猫数据框,看起来是这样的:(它基于图像分析,使用3种不同的算法及其置信度来猜测图像是什么) 最终的目标是找到最好的预测(置信度最高的预测):即第一排的狗和第二排的书 我已使用以下代码查找最大置信度和具有最大置信度的列的名称:Python 从列A中获取列名,然后将该列的值保存到新的列C中,python,pandas,Python,Pandas,我有一个熊猫数据框,看起来是这样的:(它基于图像分析,使用3种不同的算法及其置信度来猜测图像是什么) 最终的目标是找到最好的预测(置信度最高的预测):即第一排的狗和第二排的书 我已使用以下代码查找最大置信度和具有最大置信度的列的名称: df['max_conf'] = df[['p1_conf', 'p2_conf', 'p3_conf']].max(axis=1) df['max_col'] = df[['p1_conf', 'p2_conf', 'p3_conf']].idxmax(axis
df['max_conf'] = df[['p1_conf', 'p2_conf', 'p3_conf']].max(axis=1)
df['max_col'] = df[['p1_conf', 'p2_conf', 'p3_conf']].idxmax(axis=1)
df['pred_algorithm'] = df['max_col'].apply(lambda x: x.split('_')[0])
现在,我的数据帧如下所示:
p1 p1_conf p2 p2_conf p3 p3_conf max_conf pred_algorithm
dog 0.45 cat 0.32 book 0.05 0.45 p1
dog 0.01 book 0.9 table 0.5 0.9 p2
...
现在,我需要使用pred_算法列为每一行找到最佳预测
我试过这个:
df['best_prediction'] = df[df['pred_algorithm']]
我得到以下错误:
ValueError: Wrong number of items passed 1691, placement implies 1
如何创建此新列?可以使用以下列值从列中选择值: 结果数据帧:
p1 p1_conf p2 p2_conf p3 p3_conf max_conf pred_algorithm best_prediction
0 dog 0.45 cat 0.32 book 0.05 0.45 p1 dog
1 dog 0.01 book 0.90 table 0.50 0.90 p2 book
df['best_prediction'] = df.lookup(df.index, df['pred_algorithm'])
p1 p1_conf p2 p2_conf p3 p3_conf max_conf pred_algorithm best_prediction
0 dog 0.45 cat 0.32 book 0.05 0.45 p1 dog
1 dog 0.01 book 0.90 table 0.50 0.90 p2 book