0

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

7
  • 1
    Can you show us what dataset looks like? Commented Jan 7, 2020 at 9:59
  • 1
    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. Commented Jan 7, 2020 at 10:05
  • @Datanovice that is what my function categorical_fare_mean does, right ? I want to add a column with the calculated mean value for each row. So trying to achieve it by df.apply Commented Jan 7, 2020 at 10:09
  • 1
    then you can use .loc df.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? Commented Jan 7, 2020 at 10:12
  • 1
    df.loc[df['weekday']==0, 'demand_encoded'] = df['Fare'].mean() has worked, thanks !! Commented Jan 7, 2020 at 10:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.