Home > OS >  apply function to pandas grouby with some arguments
apply function to pandas grouby with some arguments

Time:09-30

I have a dataframe with 232 categories in the column 'category'. Also, I have a function which takes a dataframe as input, applies some operations on a column given as argument, and outputs a dataframe with 2 more columns

def create_delta_days(df,interval,col):
    ----df['new column 1'] = do some work here----
    ----df['new column 1'] = do some work here----
    return df

When I do df.groupby('category').apply(create_delta_days()), I get create_delta_days() missing 3 required positional arguments: 'df', 'interval', and 'col'

How do I pass arguments to finally get df with 2 more columns for each category?

CodePudding user response:

Don't use the () (extra parenthesis):

df.groupby('category').apply(lambda x: create_delta_days(x, 7, 'rank'))
  • Related