Pandas 无法更改Groupby对象

Pandas 无法更改Groupby对象,pandas,pandas-groupby,resampling,Pandas,Pandas Groupby,Resampling,我正在尝试对熊猫对象中的组重新采样。重采样工作正常,但不知怎的,对象没有被修改。。。我需要创建一个新的组还是什么 这是我的代码: grouped_by_product_comp = competitor_df.sort_values(['history_date']).groupby(['item_id']) for name, group in grouped_by_product_comp: my_prod = name group = group.drop_duplicat

我正在尝试对熊猫对象中的组重新采样。重采样工作正常,但不知怎的,对象没有被修改。。。我需要创建一个新的组还是什么

这是我的代码:

grouped_by_product_comp = competitor_df.sort_values(['history_date']).groupby(['item_id'])
for name, group in grouped_by_product_comp:
    my_prod = name
    group = group.drop_duplicates(subset = 'history_date')
    group.set_index('history_date', inplace = True)
    group = group.asfreq('D',method='pad')
    print(group.head())
    break

my_group = grouped_by_product_comp.get_group(394846296)
print(my_group.head()) 
这是我的输出:

              id    item_id  competitor_id  competitor_price
history_date                                                  
2016-01-25    3504  394846296        2301745              1205
2016-01-26    3504  394846296        2301745              1205
2016-01-27    3504  394846296        2301745              1205
2016-01-28    3504  394846296        2301745              1205
2016-01-29    3504  394846296        2301745              1205

           id history_date    item_id  competitor_id  competitor_price
187116   3504   2016-01-25  394846296        2301745              1205
188119  17460   2016-02-23  394846296        2301745              1205
188945  28392   2016-03-17  394846296        2301745              1205
189063  29988   2016-03-20  394846296        2301745              1205
189477  35004   2016-03-31  394846296        2301745              1205

所以对象在for循环之外没有改变。。。我是否应该以某种方式告诉Groupby对象更改而不是组?如果你正在读这篇文章,非常感谢

您可以使用
apply
而不是对执行循环
,并将值分配给新的数据帧(或相同的数据帧):

然后,您可以通过执行以下操作获得所需的所有数据,例如:

print (new_competitor_df[new_competitor_df['item_id'] ==394846296].head())
                id    item_id  competitor_id  competitor_price
history_date                                                  
2016-01-25    3504  394846296        2301745              1205
2016-01-26    3504  394846296        2301745              1205
2016-01-27    3504  394846296        2301745              1205
2016-01-28    3504  394846296        2301745              1205
2016-01-29    3504  394846296        2301745              1205

或者使用
print(new_competitor_df.groupby(['item_id')).get_group(394846296.head())获得相同的结果

您应该能够在循环之外实现其中的一些功能,例如,通过
历史数据和
item_id
可以将丢弃的副本作为子集。如果您提供了一些输入和所需的输出,这会有所帮助。是否仅对该组重新采样(
item_id=394846296
)?或者它将用于您所有的
项目\u id
,但您以这个为例?嗨,Alex,谢谢您的关注!我想更改对象,这意味着我在for循环内打印mygroup.head()的位置应该与我在for循环外调用它的位置具有相同的输出。我想答案可能是使用df.copy函数并将deep设置为true。@Ben,我需要更新groupby对象中所有组的设置。这不能在组外完成,因为历史日期不再是唯一的。谢谢你看我的问题!
print (new_competitor_df[new_competitor_df['item_id'] ==394846296].head())
                id    item_id  competitor_id  competitor_price
history_date                                                  
2016-01-25    3504  394846296        2301745              1205
2016-01-26    3504  394846296        2301745              1205
2016-01-27    3504  394846296        2301745              1205
2016-01-28    3504  394846296        2301745              1205
2016-01-29    3504  394846296        2301745              1205