mardi 9 octobre 2018

Descending Filtering for Dataframe

I have a dataframe such as

Index   Results  Price
0       Buy      10
1       Sell     11
2       Buy      12
3       Neutral  13
4       Buy      14
5       Sell     15

I would like to ultimately return the price difference for the first continuous combination of Buy and then Sell Results in a descending manner. Such that the first output is a difference of 1, second output 3.

for buy in df: 
        if buy:
              df['Buy Price'] = df['Price']
           for sell in df:
                  if sell:
                      df['Sell Price'] = df['Price']
                      df['Difference'] = df['Sell Price'] - df['Buy Price']

I have attempted to implement a counter but with no such luck. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire