用多个范围从嵌套的循环填充数据框

发布于 2025-02-13 19:05:06 字数 554 浏览 1 评论 0原文

我正在为建筑物盒进行一些计算(是的,将东西放入其中)。我要做的是将盒子尺寸,壁厚,盖子厚度和其他参数作为输入,并进行数学以获取我的材料分解。

我从一个功能开始,然后将其更进一步,然后用数据帧数学替换该功能,因此我可以一次计算几个框。然后我得到了越来越多的想法,最后,我正在尝试计算一定范围的尺寸和材料组合中的所有可能的框。

我遇到的问题是,我正在尝试用所有必要的输入值填充我的数据框。为此,我正在使用嵌套进行循环。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失眠症患者 2025-02-20 19:05:06

您可以使用 itertool.itertool.product.product

import pandas as pd
import numpy as np
import itertools

df = pd.DataFrame(itertools.product(np.arange(50,150,5), 
                                    np.arange(50,150,5),
                                    np.arange(50,150,5)),
                  columns = ['width', 'length', 'height']
                 )
print(df)

输出:

      width  length  height
0        50      50      50
1        50      50      55
2        50      50      60
3        50      50      65
4        50      50      70
...     ...     ...     ...
7995    145     145     125
7996    145     145     130
7997    145     145     135
7998    145     145     140
7999    145     145     145

[8000 rows x 3 columns]

说明:

>>> list(itertools.product(np.arange(1,3), np.arange(1,3),np.arange(1,3)))

[(1, 1, 1),
 (1, 1, 2),
 (1, 2, 1),
 (1, 2, 2),
 (2, 1, 1),
 (2, 1, 2),
 (2, 2, 1),
 (2, 2, 2)]

You can use itertool.product.

import pandas as pd
import numpy as np
import itertools

df = pd.DataFrame(itertools.product(np.arange(50,150,5), 
                                    np.arange(50,150,5),
                                    np.arange(50,150,5)),
                  columns = ['width', 'length', 'height']
                 )
print(df)

Output:

      width  length  height
0        50      50      50
1        50      50      55
2        50      50      60
3        50      50      65
4        50      50      70
...     ...     ...     ...
7995    145     145     125
7996    145     145     130
7997    145     145     135
7998    145     145     140
7999    145     145     145

[8000 rows x 3 columns]

Explanation:

>>> list(itertools.product(np.arange(1,3), np.arange(1,3),np.arange(1,3)))

[(1, 1, 1),
 (1, 1, 2),
 (1, 2, 1),
 (1, 2, 2),
 (2, 1, 1),
 (2, 1, 2),
 (2, 2, 1),
 (2, 2, 2)]
飘落散花 2025-02-20 19:05:06

我认为您可能可以在尺寸上进行数据框架上的十字架加入。

df = pd.DataFrame([x for x in range(50,150,5)],columns='dim') 
# this will create a dataframe with a single column called dim that is just the range
df.merge(df,how='cross')
# this gives the cartesian production - that is, all combinations, of the range in the 
# first column and the range in the second column. df has 20 rows and after the merge
# this has 400 rows
df.merge(df,how='cross').merge(df,how='cross')
# this gives the cartesian production and then the cartesian product of it again. this results
# in 8000 rows, which is 20x20x20

结果:

    dim_x   dim_y   dim
0   50        50    50
1   50        50    55
2   50        50    60
3   50        50    65
4   50        50    70
...

在您的示例中,看起来长度,宽度和高度都是相同的,但是如果它们不同,则只需将三个不同的启动数据范围合并。

I think you may be able to get away with doing a cross join on a dataframe with your dimensions.

df = pd.DataFrame([x for x in range(50,150,5)],columns='dim') 
# this will create a dataframe with a single column called dim that is just the range
df.merge(df,how='cross')
# this gives the cartesian production - that is, all combinations, of the range in the 
# first column and the range in the second column. df has 20 rows and after the merge
# this has 400 rows
df.merge(df,how='cross').merge(df,how='cross')
# this gives the cartesian production and then the cartesian product of it again. this results
# in 8000 rows, which is 20x20x20

Result:

    dim_x   dim_y   dim
0   50        50    50
1   50        50    55
2   50        50    60
3   50        50    65
4   50        50    70
...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文