将不同的换档应用于分组数据框的特定列
我有一个数据框,就像
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您为每个组(城市)的算法创建了一个字典,则可以将循环使用并转移该金额。
假设您想将其返回到数据框中:
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.
Assuming you then want to return this to the dataframe: