简体   繁体   中英

Filter a pandas DataFrame based on a boolean series with Date index?

I have a large df where the indices are dates and a boolean (True/False) series where the indices are dates as well. They do not contain the same number of rows. I want to subset my df by date where the corresponding series value is True. I have two ways of achieving this which work but I feel like there are better approaches. One involves looping and the other is basically this:

df[df.index.isin(df_sub_look[df_sub_look == True].index)]

I feel like there must be other ways and so I'm curious if there is another straightforward method that I have overlooked.

IIUC, join on the Date index, and then filter:

df = df_sub_look.to_frame(name='sub_look').join(df)
df[df.pop('sub_look')]

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