0

I have a DataFrame that I am iterating over to perform a function, like this:

for time, row in dataframe.loc[symbol].itertuples():
    self.update(time, row.close)

I know that this is not the most efficient way, and that using apply() is faster.

However, as I need to access both time and row for my update() function, how would I be able to use apply() here?

I can create a function like this:

def update(time, row):
    self.update(time, row.close)

But the part where I call apply(), not sure how to do:

dataframe.apply(update, axis=?) --> Need access to time (index) here too

Because I need both index (time) and row value for this.

All examples I've seen don't need the index.

Red Ghost
  • 302
  • 2
  • 12
  • `axis='columns'` to access the whole row but what you need too is `groupby`. Take a while to read [Group by: split-apply-combine](https://pandas.pydata.org/docs/user_guide/groupby.html) – Corralien Aug 01 '21 at 05:31
  • Can you provide an example input dataframe and the expected output? Iterating over rows it definitely not optimal and you should be able to increase speed using vectorized operation. Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – mozway Aug 01 '21 at 05:32
  • `dataframe.apply(lambda row: update(row.name, row.close), axis=1)` – ThePyGuy Aug 01 '21 at 05:32

0 Answers0