1

This may be a dumb question but I am a tad stuck with this. Say I have this dataframe:

   amount  amount_str  buy_order_id        id   midprice       price  
0  0.01545000  0.01545000     915692220  53424450  0.1286495  0.12947460   
0  1.65956203  1.65956203     915692330  53424458        NaN  0.12947460   
0  0.68427900  0.68427900     915692581  53424487        NaN  0.12947460   
0  0.22306417  0.22306417     915692632  53424491        NaN  0.12808629   
0  0.22306396  0.22306396     915692964  53424530        NaN  0.12808646   
0  2.31474046  2.31474046     915693081  53424535        NaN  0.12947460   
0  0.16808924  0.16808924     915694097  53424600        NaN  0.12808675   
0  5.30166589  5.30166589     915694819  53424629        NaN  0.12808710   
price_str  sell_order_id   timestamp  type  
0  0.12947460      915690988  1518045004     0  
0  0.12947460      915690988  1518045006     0  
0  0.12947460      915690988  1518045010     0  
0  0.12808629      915692647  1518045010     1  
0  0.12808646      915693012  1518045016     1  
0  0.12947460      915690988  1518045017     0  
0  0.12808675      915694117  1518045031     1  
0  0.12808710      915694862  1518045041     1

Here is my issue: each time the program gets a new order from bitstamp, it appends the values to this dataframe, and then adds the current midprice.

I could normally do that with df['midprice'] = value; however, this sets it for the entire column and not just that entry.

How would I make it so it adds it per line and not to the entire column?

Thanks!

1
  • 2
    "I could normally do that with df['midprice'] = value" this should never work as a method for appending to a dataframe. Commented Feb 7, 2018 at 23:18

1 Answer 1

2

Pandas specialises in vectorised calculations.

So df['midprice'] = value sets an entire series (or column) to a fixed value.

It seems like you want to amend the last midprice value only, straight after a row is added by another process. This is one way you can achieve this:

df.iloc[-1, df.columns.get_loc('midprice')] = value
Sign up to request clarification or add additional context in comments.

18 Comments

something like this was exactly what I was loking for, let me see if this works. I knew it had to do with one of the iloc, loc, or iat, at.
Is this inline btw?
@xxen0nxx, not sure what you mean by inline
hmm, im getting an error. AttributeError: 'Series' object has no attribute 'columns'
convention is df refers to dataframe. try it with the correct variable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.