I am applying a lambda function on a data frame and would like to add a new column to that dataframe, while applying the lambda function I am passing the data frame it self as a argument to the function. My code
def categorical_fare_mean(data,col,cat): print('entered once') data = data[data[col]==cat]['Fare'] fare_mean = data['Fare'].mean() return fare_mean dataset['weekday_encoded'] = dataset.apply(lambda x: categorical_fare_mean(dataset,x['weekday'],0) ,axis = 1)
This is giving me an error
KeyError: (0, 'occurred at index 0') I am not sure where I am going wrong. Can some one help me with it.