0

I am using pandas with python and I have a dataframe data. I have another dataframe missing_vals. missing_vals contains a field column and a key column. The field column contains elements that correspond to names of the columns of data i.e data.columns ~= missing_vals['field']. The mapping, however, is not one-to-one (some entries in missing_vals['field'] do not exist in data.columns. I did a set intersection operation to take care of that and got an output array result containing all the values that are both in missing_vals['field'] and data.columns. Now I want to index into data using each element of result, check to see if that column contains the value corresponding to the element in missing_vals['key'] and replace it with NaN. I tried using for-loops, but I know this is not the ideal way to do it. Is there a way to do it with vector/lambda operations or perhaps with other dataframe functions? I am new to pandas so I would really appreciate some help.

Here is my code so far:

for i in range(len(result)): field = missing_vals['field'][i] for j in range(data[field].size): if (data[field][j] == missing_vals['key'][i]): data.replace(data[field][j], np.nan)

Thanks

1
  • Please provide some sample data. For example, data.head(10) and missing_vals.head(10). Commented Jun 14, 2015 at 5:56

1 Answer 1

1

You should really post sample input/output - these things are difficult explain verbally. Anyway, I think the second loop can be done away with entirely. You really just have to do.

field = missing_vals['field'][i]
data[field].replace(missing_vals['key'][i], np.nan)

The replace method replace all occurances with the replacement value and if there are none it does nothing. It's unnecessary to loop through the columns yourself to check if the value to be replaced is there. If you post representative examples of the data frames in question I can probably help you more.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I'm sort of new to this. Next time I'll post some screenshots, thanks for the tip. Anyways, your suggestion worked! I appreciate the help.
Well you can edit the current question and then I can edit the answer. You can do it by screen shot or just by copy and pasting input/output from your interpreter. You could also vote me up if the answer helps :)

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.