我有数据集

Food Name          Protein  Calorie Carbohydrate    Calcium
Butter, salted     0.85     717       0.06          24.0
Butter, whipped    0.49     718       2.87          23.0
Butter oil         0.28     876       0.00          4.0
Cheese, blue       21.40    353       2.34          528.0
Cheese, brick      23.24    371       2.79          674.0


一排...

Protein Calorie Carbohydrate    Calcium
56      2200    130             8


哪一行与该行最匹配?

最佳答案

我相信您需要:

#get diffrence of matched columns, convert to absolute
df3 = df1[df2.columns].sub(df2.iloc[0]).abs()

#compare by minimal values, count them by sum
s = df3.eq(df3.min()).sum(axis=1)

#filter rows with maximal count
df = df1[s.eq(s.max())]
print (df)
    Food Name  Protein  Calorie  Carbohydrate  Calcium
2  Butter oil     0.28      876           0.0      4.0

10-05 21:08