2 回答
TA贡献1811条经验 获得超6个赞
这是一种方法:
w_avg = df.groupby("time").apply(lambda d: sum(d["price"]*d["volume"])/d["volume"].sum())
s = df.loc[df["time"].duplicated(keep=False),"time"].unique()
df = df.groupby("time", as_index=False).agg({"volume": "sum"})
print (df.assign(w_avg=df["time"].map(w_avg), boolean=df["time"].isin(s)))
time volume w_avg boolean
0 1599177600000 3.000 381.501760 True
1 1599177600212 3.225 381.530000 False
2 1599177600560 207.477 381.346627 True
3 1599177600590 2.586 381.450000 False
4 1599177600652 0.030 381.540000 False
5 1599177600826 0.500 381.390000 False
6 1599177601166 0.139 381.390000 False
7 1599177601304 1.445 381.390000 False
8 1599177601306 2.555 381.350000 False
9 1599177601624 1.552 381.300000 False
10 1599177601706 2.000 381.290000 False
11 1599177601868 0.262 381.310000 False
12 1599177602108 0.092 381.290000 False
13 1599177602242 0.050 381.300000 False
14 1599177602296 2.228 381.310000 False
15 1599177602312 0.050 381.320000 False
16 1599177602386 0.639 381.330000 False
17 1599177602388 20.000 381.265802 True
TA贡献1828条经验 获得超6个赞
IIUC,这是一种包含业务逻辑的方法(使用加权平均值;如果同时有买入和卖出,则向前移动一毫秒的时间戳):
# create data frame
df = pd.read_csv(StringIO(data), sep=',')
#df['timestamp'] -= df['timestamp'].min()
# find buy, sell timestamps
buy_timestamps = df.loc[ df['buy_sell'] == 'b', 'timestamp']
sell_timestamps = df.loc[ df['buy_sell'] == 's', 'timestamp']
bs_timestamps = set(buy_timestamps) & set(sell_timestamps)
# adjust timestamps
df.loc[ df['timestamp'].isin(bs_timestamps), 'timestamp' ] += 1
# helper columns
df['price_quantity'] = df['price'] * df['quantity']
df['multiple_trades'] = df.groupby('timestamp')['buy_sell'].transform('count')
现在执行聚合计算:
g = df.groupby(['timestamp', 'buy_sell'])
t = (pd.concat([
(g['price_quantity'].sum() / g['quantity'].sum()).rename('price'),
g['quantity'].sum().rename('quantity'),
g['timestamp'].count().apply(lambda x: True if x > 1 else False).rename('trade_count')],
axis=1)
.reset_index()
.filter(['timestamp', 'price', 'buy_sell', 'trade_count'])
)
print(t)
结果是:
timestamp price buy_sell trade_count
0 1599177600000 381.501760 s True
1 1599177600212 381.530000 s False
2 1599177600560 381.346627 s True
3 1599177600590 381.450000 s False
4 1599177600652 381.540000 b False
5 1599177600826 381.390000 s False
6 1599177601166 381.390000 s False
7 1599177601304 381.390000 s False
8 1599177601306 381.350000 s False
9 1599177601624 381.300000 s False
10 1599177601706 381.290000 s False
11 1599177601868 381.310000 s False
12 1599177602108 381.290000 s False
13 1599177602242 381.300000 b False
14 1599177602296 381.310000 b False
15 1599177602312 381.320000 b False
16 1599177602386 381.330000 b False
17 1599177602388 381.265802 s True
添加回答
举报