Skip to content
Advertisement

A few operations with df.groupby()

I working with a forex dataset, trying to fill in my dataframe with open, high, low, close updated every tick.

Here is my code:

JavaScript

So as you can see, with for loop I’m getting groups.

Now I want to fill the following columns in my dataframe:

  • idx be my df[‘candle_number’]
  • df[‘1h_open’] must be equal to the very first df.bid in the group
  • df[‘1h_high’] = the highest number in df.bid up until current row (so for instance if there are 350 rows in the group, for 20th value we count the highest number from 0-20 span, on 215th value we the highest value from 0-215 span which can be completely different.
  • df[‘1h_low’] = lowest value up until the current iteration (same approach as for the above)

I hope it’s not too confusing =) Cheers

Advertisement

Answer

It’s convinient to reindex on date and hour:

df_new = df.set_index(['date', 'hour'])

Then apply groupby functions aggregating by index:

JavaScript

you can reset_index() back to a flat dataframe.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement