分组并应用功能

时间:2018-10-15 09:57:06

标签: python pandas group-by apply pandas-groupby

我想按我的df“ cod_id”的变量进行分组,然后应用此功能:

[dd.loc[dd['dt_op'].between(d, d + pd.Timedelta(days = 7)), 'quantity'].sum() \
                        for d in data_1['dt_op']]

从此df中移出

print(dd)
dt_op      quantity      cod_id
20/01/18      1            613
21/01/18      8            611
21/01/18      1            613 
...

对此:

print(final_dd)
n = 7

dt_op      quantity   product_code     Final_Quantity
20/01/18      1            613               2
21/01/18      8            611               8
25/01/18      1            613               1
...

我尝试过:

dd.groupby(['cod_id']).apply([dd.loc[dd['dt_op'].between(d, d + pd.Timedelta(days = 7)), 'quantity'].sum() \
                            for d in data_1['dt_op']])

但是它引发了:

TypeError: unhashable type: 'list'

1 个答案:

答案 0 :(得分:0)

这是一个麻烦但可行的解决方案:

public class TaskModel : INotifyPropertyChanged
{
    public string Title 
    { 
      get=>_title; 
      set=>
      {
         if (value!=_title)
         {
             value=title;
             OnPropertyChanged(nameof(Title));
         }
      } 
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}