I have a pandas dataframe df with multiple columns. One of the columns is Col1 which contains float values or NaNs:
df
+----+------+-----+
| No | Col1 | ... |
+----+------+-----+
| 12 | 10 | ... |
| 23 | NaN | ... |
| 34 | 5 | ... |
| 45 | NaN | ... |
| 54 | 22 | ... |
+----+------+-----+
I run a function over Col1 excluding missing values (NaN) like this:
StandardScaler().fit_transform(df.loc[pd.notnull(df[Col1]), [Col1]])
Imagine the result is a numpy.ndarray like this:
+-----+
| Ref |
+-----+
| 2 |
| 5 |
| 1 |
+-----+
Notice that this array does not have same length than the original column Col1.
I need a solution to add the array Ref as a column to df. For each row where Col1 is NaN, the new column Ref gets NaN too.
Desired output would look like this:
+----+------+-----+-----+
| No | Col1 | ... | Ref |
+----+------+-----+-----+
| 12 | 10 | ... | 2 |
| 23 | NaN | ... | NaN |
| 34 | 5 | ... | 5 |
| 45 | NaN | ... | NaN |
| 54 | 22 | ... | 1 |
+----+------+-----+-----+