Python 如何根据pandas中其他两列中的查找值指定最小值?

Python 如何根据pandas中其他两列中的查找值指定最小值?,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,目标:以编程方式匹配两列中的组合,以找到另一列的最低值 假设我有这个: import pandas as pd d = {'Part_1': [91, 201, 201], 'Part_2': [201,111,91], 'Result': [3,3, 3], 'Sub-Score': [0.60, 0.8,0.9], 'Final-Score': [0,0,0]} df = pd.DataFrame(data=d) df 我想从子分数列中找到可

目标:以编程方式匹配两列中的组合,以找到另一列的最低值

假设我有这个:

import pandas as pd

d = {'Part_1': [91, 201, 201],
     'Part_2': [201,111,91], 
     'Result': [3,3, 3], 
     'Sub-Score': [0.60, 0.8,0.9], 
     'Final-Score': [0,0,0]}
df = pd.DataFrame(data=d)
df
我想从子分数列中找到可以分配给最终分数列的最小值。我需要根据匹配的零件_1和零件_2进行选择(这两个零件可以位于不同的位置):

在这里,我们可以看到第0行和第2行在第1部分和第2部分中有相同的值,它们完全不符合顺序。此外,我们可以看到第0行的子分数值为0.60,第2行的子分数值为0.9

我希望将第0行的子分数值(因为它是第0行第2行中最低的)分配给第0行和第2行的列最终分数第1行没有可比较的内容,并且与第0行第2行没有相同的部分,因此我们将其子分数值带入最终分数值

任何帮助都将不胜感激

(编辑):

输入:

   Final-Score  Part_1  Part_2  Result  Sub-Score
0            0      91     201       3        0.6
1            0     201     111       3        0.8
2            0     201      91       3        0.9
期望输出:

   Final-Score  Part_1  Part_2  Result  Sub-Score
0          0.6      91     201       3        0.6
1          0.8     201     111       3        0.8
2          0.6     201      91       3        0.9
我找到了一种(有点黑)似乎有效的方法:

import pandas as pd

d = {'Part_1': [91, 201, 201],
     'Part_2': [201, 111, 91],
     'Result': [3, 3, 3],
     'Sub-Score': [0.60, 0.8, 0.9],
     'Final-Score': [0, 0, 0]}
df = pd.DataFrame(data=d)

# Find lowest part-number of part-pair and add as new column
df["min_part"] = df[["Part_1", "Part_2"]].min(axis=1)
# Find highest part-number of part-pair and add as new column
df["max_part"] = df[["Part_1", "Part_2"]].max(axis=1)
print df
数据集现在看起来像:

   Final-Score  Part_1  Part_2  Result  Sub-Score  min_part  max_part
0            0      91     201       3        0.6        91       201
1            0     201     111       3        0.8       111       201
2            0     201      91       3        0.9        91       201
然后做:

# Group together rows with the same min_part, max_part pair, and assign
# their lowest "Sub-Score" value to the "Final-score" column
df["Final-Score"] = df.groupby(["min_part", "max_part"])["Sub-Score"].transform("min")
print df
最终结果:

   Final-Score  Part_1  Part_2  Result  Sub-Score  min_part  max_part
0          0.6      91     201       3        0.6        91       201
1          0.8     201     111       3        0.8       111       201
2          0.6     201      91       3        0.9        91       201
(可选)仅保留原始列:

df = df[["Final-Score", "Part_1", "Part_2", "Result", "Sub-Score"]]
print df
结果:

   Final-Score  Part_1  Part_2  Result  Sub-Score
0          0.6      91     201       3        0.6
1          0.8     201     111       3        0.8
2          0.6     201      91       3        0.9

对值进行排序,然后根据ngroup和transform min进行分组,即

temp = pd.DataFrame(pd.np.sort(df[['Part_1','Part_2']]))
grps = temp.groupby(temp.columns.tolist()).ngroup()

df['new']=df.groupby(grps)['Sub-Score'].transform('min')

   Final-Score  Part_1  Part_2  Result  Sub-Score  new
0            0      91     201       3        0.6  0.6
1            0     201     111       3        0.8  0.8
2            0     201      91       3        0.9  0.6

我也会去看一张临时桌子。首先生成一个键,然后按该键分组并应用min():

temp = pd.DataFrame(pd.np.sort(df[['Part_1','Part_2']]))
grps = temp.groupby(temp.columns.tolist()).ngroup()

df['new']=df.groupby(grps)['Sub-Score'].transform('min')

   Final-Score  Part_1  Part_2  Result  Sub-Score  new
0            0      91     201       3        0.6  0.6
1            0     201     111       3        0.8  0.8
2            0     201      91       3        0.9  0.6
# Generate a key that does not depend 
# on the order of the values in Part_1 and Part_2
df['key'] = [str(set(i)) for i in list(df[['Part_1', 'Part_2']].values)]

# Generate temporary table that contains keys and minimal values
tmp = df.groupby('key').min()['Sub-Score']

scores = {}    
for key, val in zip(tmp.index, tmp.values):
    scores[key] = val

# Place the minimal values in the original table
df.loc[:, 'Final-Score'] = [scores[key] for key in df.key]

# Finally, delete what you don't need
del df['key'], tmp

df
>   Final-Score  Part_1  Part_2  Result  Sub-Score
>0          0.6      91     201       3        0.6
>1          0.8     201     111       3        0.8
>2          0.6     201      91       3        0.9