1

I have a DataFrame with a MultiIndex of type (int, datetime). I want to set the entries of the column 'actual_12b1' that are 0.0 to NaN, but only if 'begdt' (the second level of the MultiIndex) is in 1998 or before. After some trial and error, I ended up with to following code:

year_start1999 = datetime(year=1999, month=1, day=1).date()
cond1 = data.index.get_level_values('begdt') < year_start1999
cond2 = data.actual_12b1 == 0.0
data.actual_12b1[cond1 * cond2] = np.nan

The code works, but it seems overly complicated. As I'm new to pandas (and confused by all that slicing/filtering), I thought someone might be able to suggest a cleaner way of achieving the same result.

1 Answer 1

2

You have the right idea with the indexing but swapping values will be easier with the replace method of the dataframe. eg.

date_selector = df.index.get_level_values('begdt') < pd.datetime(1999, 1, 1)
df.actual_12b1[date_selector].replace(0.0, np.nan)
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.