[英]Matching values on the list and comparing value in the columns
我正在尝试从 file2 迭代索引。 如果 file2 中的特定索引在 file1 中,接下来我想比较索引级别的“TotalFat”列中的值。 对于下面的代码,我对所有条目都有“Diff in key”,不知道为什么两个文件中的键是相同的。 我将不胜感激您的帮助!
for key in file2.index.to_list():
if key in file1.index.to_list():
if file2['TotalFat'].loc[key] == file1['TotalFat'].loc[key]:
file2['Diff'] = 'Same'
else:
file2['Diff'] = 'Diff'
else:
file2['Diff'] = 'Diff in key'
文件中的数据示例:
Key Category Item Calories TotalFat
BreakfastEgg McMuffin Breakfast Egg McMuffin 300 100
BreakfastEgg White Delight Breakfast Egg White Delight 250 8
BreakfastSausage McMuffin Breakfast Sausage McMuffin 370 23
BreakfastSausage McMuffin with Egg Breakfast Sausage McMuffin with Egg 450 28
用作索引的关键示例:“BreakfastEgg McMuffin”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.