0

I have a dataframe, filled_orders, like that:

   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

Then a loop like that:

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)

What I need to do is to do this loop until there is either one asks or one bids remaining. What I tried right above the loop is the following:

while (filled_orders.side == 'bids').empty == True or (filled_orders.side == 'asks').empty == True:

at the end what I need in the dataframe example above is the following:

   price  amount  side  fees                   timestamp
4      1       4  asks     0  2019-06-26 12:24:46.570000

Basically, no matter what input I put in that code, at the end what I need is just rows with only bids or asks

But it does not work... any idea? Thanks!

the full code looks like this:

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())

The error that I receive is the following:

AttributeError: 'NoneType' object has no attribute 'side'
Viktor.w
  • 1,787
  • 2
  • 20
  • 46

1 Answers1

1

Based on your sample data and desired output, couldn't you just:

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
help-ukraine-now
  • 3,850
  • 4
  • 19
  • 36
  • I do not really get it what does `isin` do? – Viktor.w Jul 31 '19 at 09:09
  • `isin` check if the `left_element` is in the `right_element`. I advice you to have a look at the [doc](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.isin.html) – Alexandre B. Jul 31 '19 at 09:17
  • @Viktor.w `isin` checks whether values are contained in series. You could achieve the same using `df.loc[(df['side'] == 'bids') | (df['side'] == 'asks')]`. – help-ukraine-now Jul 31 '19 at 09:18
  • @Viktor.w [this answer](https://stackoverflow.com/a/17071908/10140310) might be useful when it comes to selecting rows. Also, `.tail` is opposite of `.head` and gives you `n` last rows. Have you tested my answer on your dataset, did it help? – help-ukraine-now Jul 31 '19 at 13:47