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.
dataset
SL. No. Class Age time ... demand from_ind to_ind Fare
0 1 0 77.2 39 ... 1 5 4 11854.27
1 2 0 45.3 34 ... 1 2 4 14968.50
2 3 0 70.9 36 ... 1 1 4 5859.32
3 4 0 35.1 31 ... 1 7 4 2774.19
4 5 0 41.8 33 ... 1 7 4 3833.66
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.
I have also tried with
dataset['demand_encoded'] = dataset.apply(lambda x: categorical_fare_mean(dataset,x['demand'],1) ,axis = 1)
I got an error KeyError: (1, 'occurred at index 0')
Thanks
datasetlooks like?df.loc[df['weekday']==0]['Fare'].mean()? you don't need to use such an expensive operation for a simple thing that pandas can handle.categorical_fare_meandoes, right ? I want to add a column with the calculated mean value for each row. So trying to achieve it bydf.apply.locdf.loc[df['weekday']==0, 'demand_encoded'] = df['Fare'].mean()- change df with dataset. it's unclear what you need, as no weekday in your dataset nor have you posted what you want your data to look like?df.loc[df['weekday']==0, 'demand_encoded'] = df['Fare'].mean()has worked, thanks !!