用多个范围从嵌套的循环填充数据框
我正在为建筑物盒进行一些计算(是的,将东西放入其中)。我要做的是将盒子尺寸,壁厚,盖子厚度和其他参数作为输入,并进行数学以获取我的材料分解。
我从一个功能开始,然后将其更进一步,然后用数据帧数学替换该功能,因此我可以一次计算几个框。然后我得到了越来越多的想法,最后,我正在尝试计算一定范围的尺寸和材料组合中的所有可能的框。
我遇到的问题是,我正在尝试用所有必要的输入值填充我的数据框。为此,我正在使用嵌套进行循环。
for width in range(50,150,5):
for length in range(50,150,5):
for height in range(50,150,5):
(append to dataframe)
随着范围越来越大,数据帧变得巨大。最后,我必须花几个小时等待for循环完成并获取我的输入CSV,以便进行30秒的DF处理并获得我的结果(嵌套的循环比所示的循环更多)。
问题是,在这样的情况下,嵌套循环是填充数据的最佳方法,在这种情况下,您必须扫整一个范围并生成几个变量的组合?还是有一种更有效的方法来填充不需要这么长时间的数据框架?
I'm doing some calculations for building boxes (yeah, boxes to put stuff in). What I do is take as input the box dimensions, wall thickness, lid thickness, and other parameters and do the math to get my materials breakdown.
I started out with a function, then took it a step further and replaced the function with dataframe math, so I could calculate several boxes at once. And then I got more and more ideas, and at the end here I am trying to calculate all possible boxes in a certain range of dimensions and material combinations.
The problem I have is I'm trying to fill my dataframe with all the necessary input values. For this I'm using nested for loops.
for width in range(50,150,5):
for length in range(50,150,5):
for height in range(50,150,5):
(append to dataframe)
And as the ranges get bigger, the dataframe gets huge. And in the end I have to spend hours waiting for the for loop to complete and get my input csv, in order to do 30 seconds of df processing and get my results (there's a few more for loops nested than the ones shown).
The question, is a nested for loop the best way to fill data in a case like this, where you have to sweep a full range and generate combinations of several variables? Or is there a more efficient way to fill the dataframe that doesn't take so long?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
itertool.itertool.product.product
。输出:
说明:
You can use
itertool.product
.Output:
Explanation:
我认为您可能可以在尺寸上进行数据框架上的十字架加入。
结果:
在您的示例中,看起来长度,宽度和高度都是相同的,但是如果它们不同,则只需将三个不同的启动数据范围合并。
I think you may be able to get away with doing a cross join on a dataframe with your dimensions.
Result:
In your example, it looks like length, width, and height were all the same, but if they are different just make three different starting dataframes to merge.