我有一个熊猫数据框,在每一行存储一个食物,格式如下-
Id Calories Protein IsBreakfast IsLunch IsDinner
1 300 6 0 1 0
2 400 12 1 1 0
.
.
.
100 700 25 0 1 1
我想在以下条件下打印所有三行组合-
现在,我首先遍历所有早餐项目,选择午餐项目。然后重复所有的晚餐项目。选择一个组合后,我将添加相关列并检查值是否在所需的范围内
您可以使用
|
和&
运算符将筛选器的组合添加到数据帧。 创建虚拟数据帧,例如:输出:
现在添加所有条件:
输出:
您可以使用this answer中描述的方法生成一个新的数据帧,其中包含来自原始数据的三行的所有组合:
输出:
然后创建一个新的DataFrame,包含原始帧中所有数值字段的总和,覆盖三行的所有可能组合:
最后,您可以应用所需的任何筛选器:
相关问题 更多 >
编程相关推荐