在数据子集上使用df.iterrows()

时间:2017-07-25 08:10:34

标签: python

下面是我的数据集填充,它是通用数据集 x 的布尔子集。

fills = x.loc[(x['price'] == min_bid) & (x['bid?'] == 'T') & (x['type'] == 'trade')]

fills   
                        time    price  bid?  type    id   quantity
2509    2017-01-03 16:00:38.185 38.25   T   trade   2422    400.0
2514    2017-01-03 16:00:59.849 38.25   T   trade   2422    400.0
2623    2017-01-03 16:05:08.626 38.25   T   trade   2422    200.0
2624    2017-01-03 16:05:09.295 38.25   T   trade   2422    600.0
2749    2017-01-03 16:10:25.262 38.25   T   trade   2422    100.0
2922    2017-01-03 16:18:56.218 38.25   T   trade   2422    1500.0
2947    2017-01-03 16:20:08.441 38.25   T   trade   2422    200.0
2953    2017-01-03 16:20:38.836 38.25   T   trade   2422    400.0
2995    2017-01-03 16:25:06.441 38.25   T   trade   2422    200.0
2997    2017-01-03 16:25:07.438 38.25   T   trade   2422    1100.0
3053    2017-01-03 16:29:47.373 38.25   T   trade   2422    600.0
3064    2017-01-03 16:29:54.444 38.25   T   trade   4844    4700.0
3066    2017-01-03 16:29:54.700 38.25   T   trade   2422    100.0

我在这里想要实现的是循环遍历 fills 的索引,并且对于每一行我想从 x 获取一些值以添加到任何一个两个跑步总数。

quantity_cleared = 0
quantity_remaining = 0

for index, rows in fills.iterrows():
    offer_size = x['quantity'][(x['price'] == min_bid + tick_size) & (x['bid?'] == 'F') & (x['type'] == 'quote')].loc[:row].iloc[-1]
    trades_done = x['quantity'][(x['price'] == min_bid + tick_size) & (x['bid?'] == 'F') & (x['type'] == 'trade')].loc[row:].sum()

    if trades_done >= offer_size:
        quantity_cleared += fills['quantity']['row']
    else:
        quantity_remaining += fills['quantity']['row']

我的代码返回错误:

SystemError: <class 'TypeError'> returned a result with an error set

任何建议都将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

经过一些修补,我发现解决这个问题的方法不是找到一种方法将数据子集交叉引用到通用集,而是通过以不同的方式定义子集。所以,而不是:

for index, rows in fills.iterrows():

使用:

for index,row in x.iloc[fills.index].iterrows():