我有一个数据框,filled_orders,像这样:
price amount side fees timestamp
0 0 2 bids 0 2019-06-25 12:24:46.570000
1 3 2 asks 0 2019-06-25 12:22:46.570000
2 2 4 bids 0 2019-06-25 12:22:46.570000
3 5 1 asks 0 2019-06-25 12:24:46.570000
4 1 4 asks 0 2019-06-26 12:24:46.570000
然后这样一个循环:
for index, row in filled_orders.iterrows():
if row.side == 'asks':
filled_orders = filled_orders.drop(index)
elif row.side == 'bids':
filled_orders = filled_orders.drop(index)
我需要做的是循环执行,直到剩下一个asks
或一个bids
。我在循环上方尝试过的操作如下:
while (filled_orders.side == 'bids').empty == True or (filled_orders.side == 'asks').empty == True:
最后,在上面的数据框示例中,我需要以下内容:
price amount side fees timestamp
4 1 4 asks 0 2019-06-26 12:24:46.570000
基本上,无论我在该代码中输入了什么内容,最后我需要的只是只有bids
或asks
的行
但是这行不通...有什么主意吗?谢谢!
完整代码如下:
while (filled_orders.side == 'bids').any() == False or (filled_orders.side == 'asks').any() == False:
for index, row in filled_orders.iterrows():
if row.side == 'asks':
if filled_orders.index.contains(filled_orders.first_valid_index()):
if filled_orders.loc[index, 'amount'] == filled_orders.loc[filled_orders.first_valid_index(), 'amount'] and filled_orders.loc[filled_orders.first_valid_index(), 'side'] == 'bids':
filled_orders = filled_orders.drop(filled_orders.first_valid_index())
filled_orders = filled_orders.drop(index)
elif filled_orders.loc[index, 'amount'] < filled_orders.loc[filled_orders.first_valid_index(), 'amount'] and filled_orders.loc[filled_orders.first_valid_index(), 'side'] == 'bids':
filled_orders.at[filled_orders.first_valid_index(), 'amount'] = float(filled_orders.at[filled_orders.first_valid_index(), 'amount']) - float(filled_orders.at[index, 'amount'])
filled_orders = filled_orders.drop(index)
elif filled_orders.loc[index, 'amount'] > filled_orders.loc[filled_orders.first_valid_index(), 'amount'] and filled_orders.loc[filled_orders.first_valid_index(), 'side'] == 'bids':
filled_orders.at[index, 'amount'] = float(filled_orders.at[index, 'amount']) - float(filled_orders.at[filled_orders.first_valid_index(), 'amount'])
filled_orders = filled_orders.drop(filled_orders.first_valid_index())
if row.side == 'bids':
if filled_orders.index.contains(filled_orders.first_valid_index()):
if filled_orders.loc[index, 'amount'] == filled_orders.loc[filled_orders.first_valid_index(), 'amount'] and filled_orders.loc[filled_orders.first_valid_index(), 'side'] == 'asks':
filled_orders = filled_orders.drop(filled_orders.first_valid_index())
filled_orders = filled_orders.drop(index)
elif filled_orders.loc[index, 'amount'] < filled_orders.loc[filled_orders.first_valid_index(), 'amount'] and filled_orders.loc[filled_orders.first_valid_index(), 'side'] == 'asks':
filled_orders.at[filled_orders.first_valid_index(), 'amount'] = filled_orders.at[filled_orders.first_valid_index(), 'amount'] - filled_orders.at[index, 'amount']
filled_orders = filled_orders.drop(index)
elif filled_orders.loc[index, 'amount'] > filled_orders.loc[filled_orders.first_valid_index(), 'amount'] and filled_orders.loc[filled_orders.first_valid_index(), 'side'] == 'asks':
filled_orders.at[index, 'amount'] = float(filled_orders.at[index, 'amount']) - float(filled_orders.at[filled_orders.first_valid_index(), 'amount'])
filled_orders = filled_orders.drop(filled_orders.first_valid_index())
我收到的错误如下:
AttributeError: 'NoneType' object has no attribute 'side'
答案 0 :(得分:1)
根据您的示例数据和所需的输出,您不能只是:
df.loc[df['side'].isin(['bids', 'asks'])].tail(1)
price amount side fees timestamp
4 1 4 asks 0 2019-06-26 12:24:46.570000