8

What is a more elegant way of implementing below?

I want to apply a function: my_function to a dataframe where each row of the dataframe contains the parameters of the function. Then I want to write the output of the function back to the dataframe row.

results = pd.DataFrame()
for row in input_panel.iterrows():
    (index, row_contents) = row
    row_contents['target']  = my_function(*list(row_contents))
    results = pd.concat([results, row_contents])
cs95
  • 379,657
  • 97
  • 704
  • 746
Lisa
  • 4,126
  • 12
  • 42
  • 71
  • 1
    Could you share what `my_function` does? There probably is a way to remove the need for iteration – user3483203 May 19 '18 at 02:20
  • 1
    Somewhere it is written on Pandas Stone Tablets: [Never call DataFrame.append or pd.concat inside a for-loop. It leads to quadratic copying.](https://stackoverflow.com/a/36489724/1422451) – Parfait May 19 '18 at 03:36

1 Answers1

9

We'll iterate through the values and build a DataFrame at the end.

results = pd.DataFrame([my_function(*x) for x in input_panel.values.tolist()])

The less recommended method is using DataFrame.apply:

results = input_panel.apply(lambda x: my_function(*x))

The only advantage of apply is less typing.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • why apply is less recommended? or there is an error for the apply solution:TypeError: ('my_function() takes exactly 7 arguments (13 given)', u'occurred at index deviation') – Lisa May 19 '18 at 02:23
  • @Lisa It's slower than a loop... sometimes people like typing less and don't care about speed as long as they get their work done. And as for the error, you'll have to sort that out yourself, because you're passing more arguments than you want to, and your question did not suggest that. Without your data, there's not much more I can offer as advice. – cs95 May 19 '18 at 02:27