我需要从下面给出的示例数据框中根据以下条件查找结果:
- 通过日期时间列上的分组依据查找最大交易量的价格。
- 计算有多少个价格值低于 1 号订单的价格(忽略成交量为零的行)
我想要我的结果数据框如下:
Datetime Volume Price Count_below_prc
2020-08-05 09:15:00 1960 516 1
2020-08-05 09:20:00 1980 548 2
For Datetime = 2020-08-05 09:15:00, only one value is below 516 (504, ignoring rows with zero volume) for Datetime = 2020-08-05 09:20:00, two values are below 548 (476 & 526, ignoring rows with zero volume)
最佳答案
尝试使用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)
资源:
关于python - 根据特定条件过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66611522/