【发布时间】:2020-10-14 04:41:22
【问题描述】:
我有一个按以下方式从数据字典格式创建的数据框:
df = pd.DataFrame( info_closed, columns = [ 'type', 'origQty', 'executedQty' ] )
结果如下:
type origQty executedQty
0 LIMIT 0.00362000 0.00362000
1 MARKET 0.00200000 0.00200000
2 MARKET 0.00150000 0.00150000
3 MARKET 0.00150000 0.00150000
4 LIMIT 0.00150000 0.00150000
5 LIMIT 0.00150000 0.00150000
6 MARKET 0.00199500 0.00199500
7 LIMIT 0.00150000 0.00150000
8 MARKET 0.00149800 0.00149800
9 LIMIT 0.00150000 0.00150000
10 LIMIT 0.00149900 0.00149900
11 LIMIT 0.00150000 0.00150000
12 MARKET 0.00149800 0.00149800
[... snip ...]
我正在尝试通过以下方式创建结果:
type origQty executedQty Count
0 LIMIT 13.03 15.01 23
1 MARKET 122.0l 40.00 54
[.. snip ...]
基本上,这将是每个“类型”中的 group_by (type) 和 sum( origQty ) 和 sum ( origQty ),以及用于计算 sum( origQty ) 和 sum (origQty) 值的记录计数
我试过了:
g = df.groupby(['type'])['origQty', 'executedQty'].sum().reset_index()
但结果如下:
type origQty executedQty
0 LIMIT 0.003620000.001500000.001500000.001500000.0015... 0.003620000.001500000.001500000.001500000.0015...
1 LIMIT_MAKER 0.001499000.001500000.001500000.001500000.0014... 0.001499000.001500000.001500000.001500000.0014...
2 MARKET 0.002000000.001500000.001500000.001995000.0014... 0.002000000.001500000.001500000.001995000.0014...
3 STOP_LOSS_LIMIT 0.00150000 0.00150000
问题:我做错了什么?
TIA
预计到达时间:
感谢大家提供的解决方案!
我运行了一些,但仍然得到这种类型的输出:
origQty
executedQty
type
LIMIT_MAKER 0.001499000.001500000.001500000.001500000.0014... 0.001499000.001500000.001500000.001500000.0014...
原始数据是这样的(它是来自 Binance 交易所的数据和 ccxt 包装器代码的组合。我试图隔离 Binance 数据~仅~(与 ['info'] 相关联)
[{'info': {'symbol': 'BTCUSDT', 'orderId': 2538903025, 'orderListId': -1,'clientOrderId':'ENDsgXoqtv2ct5jizrfeQe','price':'9638.00000000','origQty':'0.00150000','executedQty':'0.00150000', 'cummulativeQuoteQty':'14.45700000','状态':'填充', 'timeInForce':'GTC','type':'LIMIT_MAKER','side':'BUY', 'stopPrice':'0.00000000','icebergQty':'0.00000000','时间': 1592879158045,'updateTime':1592879162299,'isWorking':真, 'origQuoteOrderQty':'0.00000000'},'id':'2538903025', “clientOrderId”:“ENDsgXoqtv2ct5jizrfeQe”,“时间戳”:1592879158045, '日期时间':'2020-06-23T02:25:58.045Z','lastTradeTimestamp':无, 'symbol': 'BTC/USDT', 'type': 'limit', 'side': 'buy', 'price': 9638.0, “金额”:0.0015,“成本”:14.457,“平均”:9638.0,“填充”:0.0015, “剩余”:0.0,“状态”:“关闭”,“费用”:无,“交易”:无}, {'info': {'symbol': 'BTCUSDT', 'orderId': 2539250884, 'orderListId': -1,'clientOrderId':'5UFBYwDF6b9qJ1UWNsvOYU','price':'9653.00000000','origQty':'0.00299700','executedQty':'0.00299700', 'cummulativeQuoteQty': '28.93004100', 'status': 'FILLED', 'timeInForce':'GTC','type':'LIMIT_MAKER','side':'SELL', 'stopPrice':'0.00000000','icebergQty':'0.00000000','时间': 1592883883927,'updateTime':1592884056113,'isWorking':真, 'origQuoteOrderQty':'0.00000000'},'id':'2539250884', 'clientOrderId':'5UFBYwDF6b9qJ1UWNsvOYU','时间戳':1592883883927, '日期时间':'2020-06-23T03:44:43.927Z','lastTradeTimestamp':无, 'symbol': 'BTC/USDT', 'type': 'limit', 'side': 'sell', 'price': 9653.0,“金额”:0.002997,“成本”:28.930041,“平均”:9653.0,“已填充”:0.002997,“剩余”:0.0,“状态”:“关闭”,“费用”:无, '交易':无},{'信息':{'symbol':'BTCUSDT','orderId':2539601261, 'orderListId':-1,'clientOrderId':'testme-15928890617592764', “价格”:“9633.00000000”,“原始数量”:“0.00150000”,“执行数量”: “0.00150000”、“累积报价数量”:“14.44950000”、“状态”: 'FILLED','timeInForce':'GTC','type':'LIMIT_MAKER','side':'BUY', 'stopPrice':'0.00000000','icebergQty':'0.00000000','时间': 1592889061852,'updateTime':1592889136305,'isWorking':真, 'origQuoteOrderQty':'0.00000000'},'id':'2539601261', 'clientOrderId':'testme-15928890617592764','时间戳': 1592889061852, '日期时间': '2020-06-23T05:11:01.852Z', 'lastTradeTimestamp':无,'symbol':'BTC/USDT','type':'limit', 'side': 'buy', 'price': 9633.0, 'amount': 0.0015, 'cost': 14.4495, “平均”:9633.0,“填充”:0.0015,“剩余”:0.0,“状态”: “关闭”,“费用”:无,“交易”:无}]
我通过执行以下操作将其配对:
info_closed = []
for index,item in enumerate( orders_closed ):
info_closed.append( item['info'] )
上面第一篇文章中列出了我的结果。
然后我跑了:
df = pd.DataFrame( final_output, columns = [ 'type', 'origQty', 'executedQty' ] )
我开始怀疑数据框是否有问题...将开始查看该区域...
【问题讨论】:
-
检查数据类型,可能是不是浮动的对象尝试转换为浮动。
-
当对字符串求和时,您会在输出中看到这些字符串的串联。使用
pd.to_numeric或astype将 Qty 列的 dtype 更改为数字,然后求和。
标签: python pandas group-by count sum