Substituting nan values with mean code
Here, you are substituting the missing values (nans) with something, it can be either the most frequent data, median, average(mean), whatever. mean modifies the nan values with the average of not-nan values.

for x in num_cols:
    imp = SimpleImputer(missing_values=np.nan, strategy='mean')  
    imp.fit(np.array(ds[x]).reshape(-1,1)) 
    ds[x] = imp.transform(np.array(ds[x]).reshape(-1,1))



  imp = SimpleImputer(missing_values=np.nan, strategy='mean')


For transforming missing values we use SimpleImputer, here, the missing values are np.nan and we want to use the mean strategy for transforming these nans. Thereby, their values would be substituted by the average value of each feature.


  imp.fit(np.array(ds[x]).reshape(-1,1))


You have an $n * m$ (array (np.array(ds[x]))), by reshape(-1,1)) you convert it to shape $(m*n, 1)$ -1 here stands for vertical array and 1 stands for having one column.

Then you fit your transformer (imp) to your data (np.array(ds[x]).reshape(-1,1))


  ds[x] = imp.transform(np.array(ds[x]).reshape(-1,1))


Here, the transformed data would be substituted in ds[x]