如何使用所有其余值减去第一值

发布于 2025-02-07 08:25:15 字数 1180 浏览 0 评论 0原文

我有这样的数据:

   timestamp        high    windSpeed   windDir windU   windV
04/05/2019 10:02    100      4.39      179.1    -0.14   8.53
                    150      2.44      164.5    -1.26   4.57
                    200      4.29      180.9    0.12    8.32
04/05/2019 10:03    100      4.39      179.1    -0.15   8.53
                    150      2.44      164.5    -1.26   4.57
                    200      4.29      180.9    0.12    8.32
04/05/2019 10:04    100      4.52      179.1    -0.16   8.79
                    150      2.15      162.8    -1.24   4
                    200      3.34      181.9    0.21    6.49
04/05/2019 10:05    100      4.52      179.1    -0.17   8.79
                    150      2.15      162.8    -1.24   4
                    200      3.34      181.9    0.21    6.49

我想每次都从较高级别中减去值。有人可以帮我吗?谢谢。

for timestamp, group in grouped:
    HeightIndices = group["high"].keys()
    for heightIndex in range(HeightIndices[0], HeightIndices[0] + len(HeightIndices) - 1):
        windMag = sqrt(group["windU"] ** 2 + group["windV"] ** 2)
        diffMag = windMag[heightIndex+1]-windMag[heightIndex]

I have data like this:

   timestamp        high    windSpeed   windDir windU   windV
04/05/2019 10:02    100      4.39      179.1    -0.14   8.53
                    150      2.44      164.5    -1.26   4.57
                    200      4.29      180.9    0.12    8.32
04/05/2019 10:03    100      4.39      179.1    -0.15   8.53
                    150      2.44      164.5    -1.26   4.57
                    200      4.29      180.9    0.12    8.32
04/05/2019 10:04    100      4.52      179.1    -0.16   8.79
                    150      2.15      162.8    -1.24   4
                    200      3.34      181.9    0.21    6.49
04/05/2019 10:05    100      4.52      179.1    -0.17   8.79
                    150      2.15      162.8    -1.24   4
                    200      3.34      181.9    0.21    6.49

and I want to subtract the value from higher level with lower level in each time.This is what I got so far, but this one only give me 1 value. Anyone can help me please? thank you.

for timestamp, group in grouped:
    HeightIndices = group["high"].keys()
    for heightIndex in range(HeightIndices[0], HeightIndices[0] + len(HeightIndices) - 1):
        windMag = sqrt(group["windU"] ** 2 + group["windV"] ** 2)
        diffMag = windMag[heightIndex+1]-windMag[heightIndex]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

水染的天色ゝ 2025-02-14 08:25:15

我不确定我是否要完成您的要求,但是根据我查看您的代码,看来您正在尝试在列中获得I-th和我+1-第1个索引之间的区别”高”并调用该变量diffmag。如果是这种情况,您可能可以使用两种方法之一。

解决方案1:

diff_mag = []
for i in range(len(wind['height'])-1):
    diff_mag[i] = wind['height'][i+1] - wind['height'][i]

解决方案2:
使用numpy差异。

np.diff(wind['height'])

我假设您是根据您的代码块的外观在这里使用的。希望有帮助。

编辑

好的..我想我知道你现在在说什么。

我认为这应该有效:


windMag = []
for timestamp, group in grouped:
    HeightIndices = group["high"].keys()
    for heightIndex in range(HeightIndices[0], HeightIndices[0] + len(HeightIndices) - 1):
        windMag.append(sqrt(group["windU"] ** 2 + group["windV"] ** 2))

diffMag = np.diff(windMag)

I'm not sure if I'm accomplishing what you're asking, but based on my looking at your code, it seems you are trying to get the difference between the i-th and i+1-th index in the column "high" and call that variable diffMag. If that's the case you can probably use one of the two methods.

Solution 1:

diff_mag = []
for i in range(len(wind['height'])-1):
    diff_mag[i] = wind['height'][i+1] - wind['height'][i]

Solution 2:
Use numpy diff.

np.diff(wind['height'])

I made the assumption you're using pandas here based on what your code block looks like. Hope that helps.

EDIT

Okay..I think I understand what you are saying now.

I think this should work:


windMag = []
for timestamp, group in grouped:
    HeightIndices = group["high"].keys()
    for heightIndex in range(HeightIndices[0], HeightIndices[0] + len(HeightIndices) - 1):
        windMag.append(sqrt(group["windU"] ** 2 + group["windV"] ** 2))

diffMag = np.diff(windMag)

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