Search code examples
pythondataframepandas-groupbyvalueerror

length mismatch error for groupby transform


I want to fill the missing values in the age column with the most frequent age among those paying the same fare. But it appears as if the process creates one additional index hence the length miss match error. does anyone know a way to resolve this?

concated_df['Age'] = concated_df.groupby('Fare')['Age'].transform(lambda x: x.fillna(x.mode()))

Here is the full error message- ValueError: Length mismatch: Expected axis has 1308 elements, new values have 1309 elements


Solution

  • Try This:

    # remove null values from the variable used for grouping 
    concated_df= concated_df[concated_df['Fare'].notna()]
    
    concated_df['Age'] = concated_df.groupby('Fare')['Age'].transform(lambda x: x.fillna(x.mode()))