3

I have a numpy array of size k, and a pandas dataframe with a column of size n>k that contains k missing values.

Is there an easy way to fill the k missing values from the numpy array correspondingly (that is, first occurred missing value in the column of the dataframe corresponds to the next value in the array)?

2
  • Can you please provide an example with some sample data values? Commented Feb 5, 2018 at 17:25
  • @COLDSPEED Sorry, I am new to stackoverflow, not yet familiar to the interface. Basically, I had a column of ages that contained missing values. I tried to train a classifier to predict the missing ages based on the data from other columns, after which I needed to replace the missing values of that column with the predictions. Commented Feb 5, 2018 at 17:55

1 Answer 1

2

Something like this might work. You may also want to consider what order (i.e. sorting) you want to fill these values in.

fill_values = list(range(k)) #or whatever your array is
indicies_of_missing = df[df['myColumn'].isnull()].index # list of the missing indices
for fill_index, dataframe_index in enumerate(indicies_of_missing):
    dataframe.loc[dataframe_index, 'myColumn'] = fill_values[fill_index]
Sign up to request clarification or add additional context in comments.

Comments

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.