如何在特定日期范围内对pandas列DataFrame中的某些值求和

时间:2018-01-25 19:17:19

标签: python pandas dataframe

我有一个看起来像这样的大型DataFrame: df =

    UPC   Unit_Sales  Price   Price_Change  Date 
 0   22          15    1.99         NaN     2017-10-10
 1   22          7     2.19         True    2017-10-12
 2   22          6     2.19         NaN     2017-10-13
 3   22          7     1.99         True    2017-10-16
 4   22          4     1.99         NaN     2017-10-17
 5   35          15    3.99         NaN     2017-10-09
 6   35          17    3.99         NaN     2017-10-11
 7   35          5     4.29         True    2017-10-13
 8   35          8     4.29         NaN     2017-10-15
 9   35          2     4.29         NaN     2017-10-15

基本上我试图记录一旦产品(UPC)的销售在接下来的7天价格发生变化后如何反应。我想创建一个新列[' Reaction'],它记录了从价格变化当天到7天前的单位销售总和。请记住,有时UPC的价格变化超过2,因此我希望每次价格变动都有不同的金额。 所以我想看到这个:

    UPC   Unit_Sales  Price   Price_Change  Date        Reaction
 0   22          15    1.99         NaN     2017-10-10      NaN
 1   22          7     2.19         True    2017-10-12      13   
 2   22          6     2.19         NaN     2017-10-13      NaN
 3   22          7     1.99         True    2017-10-16      11
 4   22          4     1.99         NaN     2017-10-19      NaN
 5   35          15    3.99         NaN     2017-10-09      NaN
 6   35          17    3.99         NaN     2017-10-11      NaN
 7   35          5     4.29         True    2017-10-13       15
 8   35          8     4.29         NaN     2017-10-15      NaN
 9   35          2     4.29         NaN     2017-10-18      NaN

在我的数据中如何设置日期很困难。有时候(比如UPC 35),日期不会超过7天。所以我希望它默认为下一个最近的日期,或者有多少日期(如果少于7天)。

以下是我尝试的内容: 我将日期设置为日期时间,我想通过.days方法计算天数。 这就是我考虑设置代码的方式(草稿):

  x = df.loc[df['Price_Change'] == 'True']
  for x in df: 
       df['Reaction'] = sum(df.Unit_Sales[1day :8days])

有没有更容易的方法来做到这一点,也许没有for循环?

1 个答案:

答案 0 :(得分:1)

您只需要ffill

groupby
df.loc[df.Price_Change==True,'Reaction']=df.groupby('UPC').apply(lambda x : (x['Price_Change'].ffill()*x['Unit_Sales']).sum()).values
df
Out[807]: 
   UPC  Unit_Sales  Price Price_Change        Date  Reaction
0   22          15   1.99          NaN  2017-10-10       NaN
1   22           7   2.19         True  2017-10-12      24.0
2   22           6   2.19          NaN  2017-10-13       NaN
3   22           7   2.19          NaN  2017-10-16       NaN
4   22           4   2.19          NaN  2017-10-17       NaN
5   35          15   3.99          NaN  2017-10-09       NaN
6   35          17   3.99          NaN  2017-10-11       NaN
7   35           5   4.29         True  2017-10-13      15.0
8   35           8   4.29          NaN  2017-10-15       NaN
9   35           2   4.29          NaN  2017-10-15       NaN

更新

df['New']=df.groupby('UPC').apply(lambda x : x['Price_Change']==True).cumsum().values

v1=df.groupby(['UPC','New']).apply(lambda x : (x['Price_Change'].ffill()*x['Unit_Sales']).sum())

df=df.merge(v1.reset_index())

df[0]=df[0].mask(df['Price_Change']!=True)
df
Out[927]: 
   UPC  Unit_Sales  Price Price_Change        Date  New     0
0   22          15   1.99          NaN  2017-10-10    0   NaN
1   22           7   2.19         True  2017-10-12    1  13.0
2   22           6   2.19          NaN  2017-10-13    1   NaN
3   22           7   1.99         True  2017-10-16    2  11.0
4   22           4   1.99          NaN  2017-10-17    2   NaN
5   35          15   3.99          NaN  2017-10-09    2   NaN
6   35          17   3.99          NaN  2017-10-11    2   NaN
7   35           5   4.29         True  2017-10-13    3  15.0
8   35           8   4.29          NaN  2017-10-15    3   NaN
9   35           2   4.29          NaN  2017-10-15    3   NaN