0

I want to create two functions, apply those functions on the DataFrame, and return the result to column interval_ratio

import seaborn as sns
import pandas as pd
import numpy as np
max_testing_data = sns.load_dataset('geyser')
max_testing_data = max_testing_data[max_testing_data.groupby('waiting')['duration'].transform('max') == max_testing_data['duration']]
median = max_testing_data.groupby('kind', as_index=False)['waiting'].median()
print(median)
       
def short_modifier(waiting, duration):
    max_testing_data['interval_ratio'] = max_testing_data['duration']/max_testing_data['waiting']

def long_modifier(duration, waiting):
    max_testing_data['interval_ratio'] = max_testing_data['waiting']/max_testing_data['duration']

max_testing_data.apply(short_modifier, axis=0)
max_testing_data.apply(long_modifier, axis=0)

I am getting an error:

short_modifier() missing 1 required positional argument: 'duration'

How can I fix this?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • Does this answer your question? [python pandas: apply a function with arguments to a series](https://stackoverflow.com/questions/12182744/python-pandas-apply-a-function-with-arguments-to-a-series) – nathan liang Mar 27 '22 at 21:11

1 Answers1

0

You haven't said which line the error is from, but am I corrent to believe it is the penultimate one? As Nathan directs, the problem is that you are missing the argument "duration" for you function. More information about .apply() can be found here.

Without needing to use .apply(), you could instead write the following:

max_testing_data["interval_ratio"] = max_testing_data["duration"] / max_testing_data["waiting"]

This will produce the same result as the function short_modifier().

Rawson
  • 2,637
  • 1
  • 5
  • 14