有日期限制的总和信息

时间:2019-04-20 14:11:55

标签: python pandas filter group-by

我只想根据日期范围对ID和给定值进行总计

我尝试过:

class cellview: UITableViewCell {


    @IBOutlet weak var programnames: UIButton!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

df:

days=(360)
import datetime 
import pandas as pd
group = None
df=pd.read_excel(r'C:\Users\xxxx\Desktop\xxxxx.xlsx')
for i in days:
        D = ((datetime.datetime.now()-datetime.timedelta(days=i)))
        B = D <= df['DATE']
        group = df.groupby('ID')['VALUE'].filter(lambda x: B ).sum()

但是有错误

DATE       ID  VALUE
2019-01-01 1    10
2019-02-01 2    15
2019-03-01 1    12

即使B的类型也是bool

我需要的结果是:

TypeError: the filter must return a boolean result

1 个答案:

答案 0 :(得分:0)

我相信您需要在groupby之前通过boolean indexing进行过滤:

 group = df[B].groupby('ID')['VALUE'].sum()

或者:

 D = ((datetime.datetime.now()-datetime.timedelta(days=i)))
 B = df.loc[D <= df['DATE'], 'VALUE']
 group = B.groupby(df['ID']).sum()