Question: ```
function deployAddRecs(){
    for (let i = 0; i < vanArr.length; i++){
        addRecAjax.push(
            $.get(url, {
                act: "API_AddRecord",
                _fid_8: "Unplugged",
                _fid_10: vanArr[i]
            })
        );
    }
    $.when(addRecAjax).done(function(){
        window.location.reload();
    });
}

```

Hey everyone, I'm having a very frustrating problem ensuring all of the ajax requests in my array are completed before proceeding.

From the code snippet provided above, I am utilizing a database's API to add records to a table with specific information in the record. From my understanding of the documentation, the callback function is executed in .done() once all the requests in the array are successful.

However, the page is reloading prior to requests being completed, because from observation the page reloads before all records are added. Any help would be amazing.

Answer: 
It's likely an initial casting error (I had some strings I needed to be floats). To convert all cols to floats do:

```
from pyspark.sql.functions import col
df = df.select(*(col(c).cast("float").alias(c) for c in df.columns))

```

Then you should be fine to impute. Note: I set my strategy to median rather than mean.

```
from pyspark.ml.feature import Imputer

imputer = Imputer(
    inputCols=df.columns, 
    outputCols=["{}_imputed".format(c) for c in df.columns]
    ).setStrategy("median")

# Add imputation cols to df
df = imputer.fit(df).transform(df)

```
I'd be interested in a more elegant solution but I separately imputed the categoricals from the numerics. To impute the categoricals I got the most common value and filled the blanks with it using the `when` and `otherwise` functions:

```
import pyspark.sql.functions as F
for col_name in ['Name', 'Gender', 'Profession']:
    common = df.dropna().groupBy(col_name).agg(F.count("*")).orderBy('count(1)', ascending=False).first()[col_name]
    df = df.withColumn(col_name, F.when(F.isnull(col_name), common).otherwise(df[col_name]))

```

To impute the numerics before running the imputer lines I simply casting the Age and Id columns as doubles circumvents the issue for the numeric fields and restrict the imputer to the numerical columns.

```
from pyspark.ml.feature import Imputer
df = df.withColumn("Age", df['Age'].cast('double')).withColumn('Id', df['Id'].cast('double'))
imputer = Imputer(
inputCols=['Id', 'Height', 'Age'],
outputCols=['Id', 'Height', 'Age'])
imputer.fit(df).transform(df)

```