将不同的换档应用于分组数据框的特定列

发布于 2025-01-31 04:15:24 字数 1091 浏览 2 评论 0原文

我有一个数据框,就像

In [1]: features

Out[1]:
                             ndvi_ne ndvi_sw   precipitation_amt_mm  reanalysis_air_temp_k
city    year    weekofyear
sj      1990    18           0.122600 0.10372  12.42                 297.572857
                19           0.145601 0.16920  10.02                 295.514578
                20           0.110496 0.00366  4.15                  299.174907
...     ...     ...          ...      ...      ...                   ...
iq      2010    20           0.197400 0.05218  11.45                 295.508559
                21           0.173843 0.05270  17.02                 296.062779
                22           0.051905 0.74994  10.15                 297.073854

我想要索引“城市”的数据框架,并通过让我们说2 reanalysis_air_temp_k 'sj'组的列 。并说1 'IQ'组的相同列

但是,

features.groupby('city')['precipitation_amt_mm'].shift(1)

“ SJ”和'IQ'组的chift ['sward_amt_mm']列以1的形式移动,

似乎GroupBy方法没有娱乐性将不同的移动应用于不同组的同一列的不同偏移。

关于如何完成任务的任何想法或方法将受到赞赏。

I have a dataframe like

In [1]: features

Out[1]:
                             ndvi_ne ndvi_sw   precipitation_amt_mm  reanalysis_air_temp_k
city    year    weekofyear
sj      1990    18           0.122600 0.10372  12.42                 297.572857
                19           0.145601 0.16920  10.02                 295.514578
                20           0.110496 0.00366  4.15                  299.174907
...     ...     ...          ...      ...      ...                   ...
iq      2010    20           0.197400 0.05218  11.45                 295.508559
                21           0.173843 0.05270  17.02                 296.062779
                22           0.051905 0.74994  10.15                 297.073854

I want groupby the dataframe by index 'city' and shift by lets say 2 the reanalysis_air_temp_k column of the 'sj' group. And shift by lets say 1 the same column of the 'iq' group.

But here

features.groupby('city')['precipitation_amt_mm'].shift(1)

shifts ['precipitation_amt_mm'] column of both 'sj' and 'iq' groups by 1

It seems that groupby method doesn't have funtionality to apply different shifts to the same column of different groups.

Any idea or method is appreciated on how to do the task.

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

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

发布评论

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

评论(1

左耳近心 2025-02-07 04:15:24

如果您为每个组(城市)的算法创建了一个字典,则可以将循环使用并转移该金额。

shifts = {"sj": 2, "iq": 1}

for grp, vals in features.reset_index(drop=False).set_index("city").groupby("city"):
    print(vals["precipitation_amt_mm"].shift(shifts[grp]))

假设您想将其返回到数据框中:

import itertools

# chain the list of lists to one list
features["precipitation_amt_mm"] = list(itertools.chain(
    # create a list for each groupby group (in "city")
    # shifting the "precipitation_amt_mm" col using dictionary
    *[list(vals["precipitation_amt_mm"].shift(shifts[grp]).values) \
      # for each group in the dataframe, reseting the multi-index to just "city" temporarily
      for grp, vals in features.reset_index(drop=False).set_index("city").groupby("city")]))

#                       ndvi_ne ndvi_sw  precipitation_amt_mm  reanalysis_air_temp_k
#city year weekofyear                                                               
#sj   1990 18          0.122600 0.10372                   NaN             297.572857
#          19          0.145601 0.16920                 11.45             295.514578
#          20          0.110496 0.00366                 17.02             299.174907
#iq   2010 20          0.197400 0.05218                   NaN             295.508559
#          21          0.173843 0.05270                   NaN             296.062779
#          22          0.051905 0.74994                 12.42             297.073854

If you create a dictionary for the amount to shift for each group (city), you can use a for loop and shift by that amount.

shifts = {"sj": 2, "iq": 1}

for grp, vals in features.reset_index(drop=False).set_index("city").groupby("city"):
    print(vals["precipitation_amt_mm"].shift(shifts[grp]))

Assuming you then want to return this to the dataframe:

import itertools

# chain the list of lists to one list
features["precipitation_amt_mm"] = list(itertools.chain(
    # create a list for each groupby group (in "city")
    # shifting the "precipitation_amt_mm" col using dictionary
    *[list(vals["precipitation_amt_mm"].shift(shifts[grp]).values) \
      # for each group in the dataframe, reseting the multi-index to just "city" temporarily
      for grp, vals in features.reset_index(drop=False).set_index("city").groupby("city")]))

#                       ndvi_ne ndvi_sw  precipitation_amt_mm  reanalysis_air_temp_k
#city year weekofyear                                                               
#sj   1990 18          0.122600 0.10372                   NaN             297.572857
#          19          0.145601 0.16920                 11.45             295.514578
#          20          0.110496 0.00366                 17.02             299.174907
#iq   2010 20          0.197400 0.05218                   NaN             295.508559
#          21          0.173843 0.05270                   NaN             296.062779
#          22          0.051905 0.74994                 12.42             297.073854
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文