简体   繁体   中英

Slice a DataFrame on second level of MultiIndex that is a date

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM