我需要从下面给出的示例数据框中按照以下条件找到结果:
约会时间 | 体积 | 价格 |
---|---|---|
2020-08-05 09:15:00 | 1033 | 504 |
2020-08-05 09:15:00 | 1960年 | 516 |
2020-08-05 09:15:00 | 0 | 450 |
2020-08-05 09:15:00 | 1724 | 520 |
2020-08-05 09:15:00 | 0 | 500 |
2020-08-05 09:15:00 | 1870年 | 540 |
2020-08-05 09:20:00 | 1024 | 476 |
2020-08-05 09:20:00 | 1980 | 548 |
2020-08-05 09:20:00 | 0 | 480 |
2020-08-05 09:20:00 | 1426 | 526 |
2020-08-05 09:20:00 | 0 | 586 |
2020-08-05 09:20:00 | 1968年 | 588 |
我希望我的结果数据框如下:
Datetime Volume Price Count_below_prc
2020-08-05 09:15:00 1960 516 1
2020-08-05 09:20:00 1980 548 2
对于Datetime = 2020-08-05 09:15:00,对于Datetime = 2020-08-05 09:20:00,只有一个值低于516(504,忽略零体积的行),对于两个值低于548(476 &526,忽略零体积的行)
尝试使用groupby
和apply
def func(row):
x = (row[row['Volume']==max(row['Volume'])])
x['Count_below_prc']=(row.loc[row['Price'].lt(x['Price'].values[0]) & row['Volume'].ne(0), 'Price'].count())
return x
res = df.groupby('Datetime',as_index=False).apply(func).reset_index(drop=True)
分辨率:
约会时间 | 体积 | 价格 | Count_below_prc | |
---|---|---|---|---|
0 | 2020-08-05 09:15:00 | 1960年 | 516 | 1个 |
1个 | 2020-08-05 09:20:00 | 1980 | 548 | 2个 |
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句